23,120
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
static int flag;
void test()
{
pid_t pid;
pid = fork();
if (pid && !flag++)
pthread_cond_wait(&cond, &mutex);
if (0 == pid)
printf("Child process\n");
else if (pid - EOF)
printf("Dad process\n");
else
exit(EXIT_FAILURE);
if (0 == pid && flag++)
pthread_cond_signal(&cond);
}
int main()
{
printf("main function\n");
test();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
sem_t * sem;
static int flag;
void test()
{
pid_t pid;
pid = fork();
if (pid && !flag++)
sem_wait(sem);
if (0 == pid)
printf("Child process\n");
else if (pid - EOF)
printf("Dad process\n");
else
exit(EXIT_FAILURE);
if (0 == pid && flag++)
sem_signal(sem);
}
int main()
{
sem = sem_open("sem", O_CREAT, 0644, 0);
printf("main function\n");
test();
sem_close(sem);
return 0;
}