Linux实验一

#include<stdio.h>

#include<unistd.h>

#include<stdlib.h>

#include<sys/types.h>

int g_var=100;

int main()

{

int var=100;

int pid=fork();

if(pid==0)

{

while(1)

{

g_var++;

var++;

printf("%d %d %d %d\n",g_var,var,getppid(),getpid());

if(g_var>1000||var>1000) break;

sleep(1);

}

}

else

{

sleep(1);

printf("%d %d %d %d\n",g_var,var,getppid(),getpid());

}

return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <semaphore.h>

#include <pthread.h>

// 声明一个 POSIX 信号量

sem_t semaphore;

// 共享资源

int shared_resource = 0;

// 线程函数

void *thread_function(void *arg) {

int i;

for (i = 0; i < 5; i++) {

// 等待信号量

sem_wait(&semaphore);

// 临界区,访问共享资源

shared_resource++;

printf("Thread: shared_resource = %d\n", shared_resource);

// 释放信号量

sem_post(&semaphore);

}

pthread_exit(NULL);

}

int main() {

// 初始化信号量

if (sem_init(&semaphore, 0, 1) != 0) {

perror("Semaphore initialization failed");

exit(EXIT_FAILURE);

}

pthread_t thread;

// 创建线程

if (pthread_create(&thread, NULL, thread_function, NULL) != 0) {

perror("Thread creation failed");

exit(EXIT_FAILURE);

}

// 等待线程结束

pthread_join(thread, NULL);

// 销毁信号量

sem_destroy(&semaphore);

return 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

#include <unistd.h>

#define NUM_PHILOSOPHERS 5

sem_t forksNUM_PHILOSOPHERS;

void *philosopher(void *arg) {

int id = *(int *)arg;

int left_fork = id;

int right_fork = (id + 1) % NUM_PHILOSOPHERS;

while (1) {

// 思考

printf("Philosopher %d is thinking\n", id);

sleep(rand() % 3);

// 拿起左边的叉子

sem_wait(&forksleft_fork);

printf("Philosopher %d picked up fork %d (left)\n", id, left_fork);

// 拿起右边的叉子

sem_wait(&forksright_fork);

printf("Philosopher %d picked up fork %d (right)\n", id, right_fork);

// 吃饭

printf("Philosopher %d is eating\n", id);

sleep(rand() % 3);

// 放下左边的叉子

sem_post(&forksleft_fork);

printf("Philosopher %d put down fork %d (left)\n", id, left_fork);

// 放下右边的叉子

sem_post(&forksright_fork);

printf("Philosopher %d put down fork %d (right)\n", id, right_fork);

}

}

int main() {

pthread_t philosophersNUM_PHILOSOPHERS;

int idsNUM_PHILOSOPHERS;

// 初始化叉子的信号量

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {

sem_init(&forksi, 0, 1);

}

// 创建哲学家线程

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {

idsi = i;

pthread_create(&philosophersi, NULL, philosopher, &idsi);

}

// 等待线程结束

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {

pthread_join(philosophersi, NULL);

}

// 销毁叉子的信号量

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {

sem_destroy(&forksi);

}

return 0;

}

相关推荐
IT大白鼠7 分钟前
彭大帅的AI运维助手——自然语言管理 Linux 集群与网络设备——第 0 篇 · 导读:把 Linux 运维交给 AI,到底靠谱吗
linux·运维·人工智能
AI智讯中枢28 分钟前
高性能 C++ 实战 (五):perf+FlameGraph 火焰图生产实战,精准定位 CPU / 缓存 / 锁瓶颈,避坑 + 完整实操案例
linux·c++·性能调优·性能分析·perf·flamegraph·火焰图
xbzb1 小时前
Linux SELinux 安全加固与标签排错手册
linux·运维·安全
江屿风1 小时前
【Linux系统】【从【收尾】缓冲区到【新开】磁盘块:一节课打通文件系统底层原理】流食般投喂
linux·运维·服务器·人工智能·笔记
Pointer Pursuit2 小时前
编辑器Vim
linux·编辑器·vim
_upupup2 小时前
Linux中的权限解析
linux·服务器
路漫漫其修远兮sjw2 小时前
Linux 运维知识点笔记(中级)
linux·运维·笔记
石一峰6992 小时前
Linux INPUT 与 I2C 子系统关联
linux·运维·计算机外设
宵时待雨2 小时前
linux笔记归纳24:多路转接select
linux·服务器·笔记·高并发
RISCV_Explorer2 小时前
技术指南:从总线协议到软件可见行为——RISC-V 多核缓存一致性的 Linux 实践
linux·缓存·risc-v