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 forks[NUM_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(&forks[left_fork]);

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

// 拿起右边的叉子

sem_wait(&forks[right_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(&forks[left_fork]);

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

// 放下右边的叉子

sem_post(&forks[right_fork]);

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

}

}

int main() {

pthread_t philosophers[NUM_PHILOSOPHERS];

int ids[NUM_PHILOSOPHERS];

// 初始化叉子的信号量

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

sem_init(&forks[i], 0, 1);

}

// 创建哲学家线程

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

ids[i] = i;

pthread_create(&philosophers[i], NULL, philosopher, &ids[i]);

}

// 等待线程结束

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

pthread_join(philosophers[i], NULL);

}

// 销毁叉子的信号量

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

sem_destroy(&forks[i]);

}

return 0;

}

相关推荐
一位摩羯座DBA42 分钟前
Redhat&Centos挂载镜像
linux·运维·centos
学习3人组42 分钟前
CentOS配置网络
linux·网络·centos
weixin_307779131 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
漫步企鹅2 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win2 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、2 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
冰橙子id3 小时前
linux系统安全
linux·安全·系统安全
stark张宇3 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao3 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
悲伤小伞3 小时前
linux_git的使用
linux·c语言·c++·git