23,121
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <fcntl.h>
int semid; //全局变量
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO
(Linux specific) */
};
int sem_p(void);
int sem_v(void);
int sem_init(void);
void sem_del(void);
int main(int argc, char *argv[])
{
pid_t pid;
pid = fork();
if (pid == -1)
{
perror("fork");
exit(-1);
}
else if (pid == 0) //child process
{
}
else //parent process
{
}
return 0;
}
int sem_init(void) //信号量初始化
{
union semun sem;
sem.val = 10;
if (semctl(semid, 0, SETVAL, sem) == -1)
{
perror("semctl");
exit(-1);
}
return 0;
}
int sem_p(void) //P操作
{
num--;
struct sembuf sem;
sem.sem_num = 0;
sem.sem_op = -1;
sem.sem_flg = SEM_UNDO;
if (semop(semid, &sem, 1) == -1)
{
perror("semop p");
exit(-1);
}
printf("还有%d个\n",num);
fflush(stdout);
return 0;
}
int sem_v(void) //V操作
{
num++;
struct sembuf sem;
sem.sem_num = 0;
sem.sem_op = +1;
sem.sem_flg = SEM_UNDO;
if (semop(semid, &sem, 1) == -1)
{
perror("semop v");
exit(-1);
}
return 0;
}
void sem_del(void) //删除信号
{
union semun semval;
if (semctl(semid, 0, IPC_RMID, semval) == -1)
{
perror("semdel");
exit(-1);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#define NUM 20
int main()
{
int i, fd, index;
sem_t *sem_ptr;
pid_t pids[NUM], pid;
fd = open("/dev/zero", O_RDWR);
sem_ptr = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
sem_init(sem_ptr, 1, 10);
for (i = 0; i < NUM; i++)
{
index = i + 1;
if((pids[i] = fork()) > 0)
continue; //parent continue fork
//child
sem_wait(sem_ptr);
printf("I'm Num.%d, I have got a boat!\n", index);
sleep(1);
sem_post(sem_ptr);
printf("I'm Num.%d, I have given back the boat!\n", index);
exit(0);
}
while ((pid = waitpid(-1, NULL, 0)) > 0)
;
puts("GAME OVER!");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM 20 //20个客户
sem_t sem;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int count = 0;
void * thread_routine(void *arg);
int main()
{
int i;
pthread_t tid;
sem_init(&sem, 0, 10);
for (i = 0; i < NUM; i++)
{
pthread_mutex_lock(&mutex);
count++;
pthread_mutex_unlock(&mutex);
if(pthread_create(&tid, NULL, thread_routine, (void *)(i+1)) != 0)
perror("Cann't create thread:");
}
pthread_mutex_lock(&mutex);
while (count != 0)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
puts("GAME OVER!");
return 0;
}
void * thread_routine(void *arg)
{
int num = (int)arg;
pthread_detach(pthread_self());
sem_wait(&sem);
printf("I'm Num.%d, I have got a boat!\n", num);
sleep(1);
sem_post(&sem);
pthread_mutex_lock(&mutex);
count--;
pthread_mutex_unlock(&mutex);
printf("I'm Num.%d, I have given back the boat!\n", num);
pthread_cond_signal(&cond);
return;
}