在一些exemples我沒有看到,他們使用線程爲 ,但我想只用叉子和1個私人旗語專用信號燈在C
我想要做一些簡單的這樣做:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<semaphore.h>
int main() {
pid_t pid;
sem_t sema;
sem_init(&sema,1,0);
pid=fork();
if(pid==0) {
printf("first"); // want this first
sem_post(&sema);
}else{
sem_wait(&sema);
printf("second -_-");// and that second
}
return 0;
}
///////////////////////////////////
GCC pgm.c -pthread 。 /a.out
///////////////////// //////////////
輸出: 第一
//////////////////////// //////////
所以最新的問題在這裏?我希望它=>
第一第二-_-
或使用現代接口'shm_open',這是一個更簡單的使用 – 2014-12-03 21:03:31