我在子进程中访问信号量时遇到问题。在子进程中,我无法通过semget()获取已创建的信号量。
这是make_semaphores()函数;
int make_semaphores(key_t key)
{
int semid;
semid = semget(key, 3, IPC_CREAT|/*IPC_EXCL|*/0666);
if(semid == -1)
{
perror("Creating an array of semaphores");
exit(1);
}
if(semctl(semid,0,SETVAL, (int)MAX)==-1)
{
perror("Initializing 'empty' semaphore");
exit(1);
}
if(semctl(semid,1,SETVAL, (int)0)==-1)
{
perror("Initializing 'full' semaphore");
exit(1);
}
if(semctl(semid,2,SETVAL, (int)1)==-1)
{
perror("Initializing mutex");
exit(1);
}
return semid;
}
这是allocate_memory函数:
int* allocate_memory(int *buf, key_t key)
{
int shmid;
shmid=shmget(key,(MAX+1)*sizeof(int),IPC_CREAT|/*IPC_EXCL|*/0666);
if(shmid==-1)
{
perror("Creating shared memory segment");
exit(1);
}
buf=(int*)shmat(shmid,NULL,0);
if(buf==NULL)
{
perror("Including shared memory segment");
exit(1);
}
buf[0]=0;
return buf;
}
我搜索过并没有找到令人满意的答案,如果我不必要地发送垃圾邮件,那就很抱歉。提前感谢您的任何帮助。
这是完整的主要内容:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define S1KEY 467221
#define S2KEY 379231
#define S3KEY 217411
#define MAX 10
int main(int argc, char* argv[])
{
srand(time(NULL));
int fork_id;
int *buf1, *buf2, *buf3;
int semid1, semid2, semid3, i, j, r, smd1;
int prod_info[3]={0,0,0}, cons_info[3]={0,0,0};
printf("-------------------\n");
buf1=allocate_memory(buf1,S1KEY);
buf2=allocate_memory(buf2,S2KEY);
buf3=allocate_memory(buf3,S3KEY);
printf("-------------------\n");
semid1=make_semaphores(S1KEY);
semid2=make_semaphores(S2KEY);
semid3=make_semaphores(S3KEY);
smd1=semget(S1KEY,3,0666);//here is okay
if(semid1!=smd1)
{
printf("semid1: %d smd1: %d\n",semid1,smd1);
perror("S1KEY does not exist");
}
printf("----------------\n");
for(i=0;i<4;++i)
{
fork_id=fork();
if(fork_id<0)
{
perror("fork()");
exit(1);
}
else if(fork_id==0)// one of four children
{
smd1=semget(S1KEY,3,0666);//here is NOT okay, smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
if(i==3) //producer for 3 buffers
{
for(j=0;j<100;j++)
{
r=(rand()%3)+1;
printf("-------------------------\n");
if(r==1)
{
printf("Prod:1||%d||%d\n",prod_info[0],getval_semaphores(semid1,0));
printf(" %d\n", buf1[0]);
produce(buf1,semid1,i);
prod_info[0]++;
}
else if(r==2)
{
printf("Prod:2||%d||%d\n",prod_info[1],getval_semaphores(semid2,0));
printf(" %d\n", buf2[0]);
produce(buf2,semid2,i);
prod_info[1]++;
}
else
{
printf("Prod:3||%d||%d\n",prod_info[2],getval_semaphores(semid3,0));
printf(" %d\n", buf3[0]);
produce(buf3,semid3,i);
prod_info[2]++;
}
}
printf("Produced: 1:%d 2:%d 3:%d\n", prod_info[0],prod_info[1],prod_info[2]);
exit(1);
}
else if(i==2) //3 consumers for 3 buffers (1,2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:3\n");
printf("%d\n", buf1[0]);
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[0]=consume(buf1,semid1);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf1: %d\n",cons_info[0]);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==1) //2 consumers for 2 buffers (2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:2\n");
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==0) //1 consumer for 1 buffer (3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:1\n");
printf("%d\n", buf3[0]);
cons_info[2]=consume(buf3,semid3);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
}
else
{
if(i==4) //Parent, waits after making children
{
smd1=semget(S1KEY,3,0666);
if(semid1!=smd1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
printf("Parent\n");
wait(NULL);
}
}
}
break_semaphores(semid1);
break_semaphores(semid2);
break_semaphores(semid3);
return 0;
}
答案 0 :(得分:1)
我担心你会误解过程的创建是如何运作的。
父/子流程模型
流程创建模型一般如下
pid_t p1 = fork();
if (p1 < 0) {
perror("problem forking");
}
else if (p1 == 0) {
// parent process
printf("parent process\n");
// do_parent_process_stuff()
}
else {
// child process
printf("child process: %d\n", p1);
// do_child_process_stuff()
}
更正您的代码
至于你的代码,你需要在主函数中替换
for(i=0;i<4;++i)//parent creates four children
{
fork_id=fork();
smd1=semget(S1KEY,3,0666);//here is NOT okay (when in child process), smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
//the rest
有这样的东西
/* Start children. */
for (i = 0; i < n; ++i) {
if ((pids[i] = fork()) < 0) {
perror("error forking");
}
// parent
else if (pids[i] == 0) {
exit(0);
}
// child process
else {
smd1=semget(S1KEY,3,0666);
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
else {
// do stuff with semaphores
}
}
}