我是一个C菜鸟试图创建一个使用semget的程序(如果是main.c的声明,则非常第一)。我在linux机器上工作并编写了一些涉及信号量的代码。当我运行我的程序时,它返回“semget:没有这样的文件或目录”。我没有包含carbon.c和hydrogen.c文件,但如果有人认为他们对问题是必要的话,我会把它们放好。谢谢你的帮助。
#include "main.h"
void hydrogen(void);//executes hydrogen.c
void carbon(void);//executes carbon.c
int main() {
int semid, shmid;//semaphore memory id, shared memory id
unsigned short seminit[NUM_SEMS];//used to initialize semaphores
struct common *shared;//pointer to shared data structure
union semun semctlarg;//used to initialize semaphores
//get semaphore memory id
if ((semid = semget(SEMKEY, NUM_SEMS, 0777)) < 0) {
perror("semget");
exit(EXIT_FAILURE);
}
seminit[MUTEX] = 1;//initialize mutex semaphore count to 1
seminit[SH] = 0;//initialize hyrdrogen semaphore count to 0
seminit[SC] = 0;//initialize carbon semaphore count to 0
semctlarg.array = seminit;//set control array
//apply initialization
if ((semctl(semid, NUM_SEMS, SETALL, semctlarg)) < 0) {
perror("semctl");
exit(EXIT_FAILURE);
}
//get shared memory id
if ((shmid = shmget(SHMKEY, 1*K, 0777)) < 0) {
perror("shmget");
exit(EXIT_FAILURE);
}
//retrieve pointer to shared data structure
if ((shared = (struct common *)shmat(shmid, 0, 0)) < 0) {
perror("shmat");
exit(EXIT_FAILURE);
}
//initialize shared data structure variables
shared->waiting_H = 0;
shared->waiting_C = 0;
int retVal;//used to check return value of fork()
// spawn 20 Hydrogens
for (int i=0; i<NUM_H; i++) {
if ((retVal = fork()) == 0) {
hydrogen();
fflush(stdout);
printf("New Hydrogen process created\n");
fflush(stdout);
} else if (retVal < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
}
// spawn 5 Carbons
for (int i=0; i<NUM_C; i++) {
if ((retVal = fork()) == 0) {
carbon();
fflush(stdout);
printf("New Hydrogen process created\n");
fflush(stdout);
} else if (retVal < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
}
//wait for all car processes to finish
for (int i = 0; i < 25; ++i) {
if (wait(0) < 0) {
perror("wait");
exit(EXIT_FAILURE);
}
}
printf("All atoms have crossed!\n");
//delete semaphores
if (semctl(semid, NUM_SEMS, IPC_RMID, 0) < 0) {
perror("semctl");
exit(EXIT_FAILURE);
}
//delete shared memory
if (shmctl(shmid, IPC_RMID, 0) < 0) {
perror("shmctl");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
void carbon(void) {
execl("carbon", "carbon", 0);
perror("execl");
exit(EXIT_FAILURE);//if exec returns there was an error
}
void hydrogen(void) {
execl("hydrogen", "hydrogen", 0);
perror("execl");
exit(EXIT_FAILURE);//if exec returns there was an error
}
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define SEMKEY 77
#define SHMKEY 77
#define NUM_SEMS 3
#define MUTEX 0
#define SH 1
#define SC 2
#define NUM_C 5
#define NUM_H 20
#define K 1024//used for shared memory creation
enum MoleculeName { Carbon, Hydrogen };
//used to initialize semaphores
union semun {
unsigned short *array;
};
//shared data structure among processes
struct common {
int waiting_C;
int waiting_H;
};
void semWait(int semid, int semaphore) {
struct sembuf psembuf;
psembuf.sem_op = -1;
psembuf.sem_flg = 0;
psembuf.sem_num = semaphore;
semop(semid,&psembuf,1);
return;
}
void semSignal(int semid, int semaphore) {
struct sembuf vsembuf;
vsembuf.sem_op = 1;
vsembuf.sem_flg = 0;
vsembuf.sem_num = semaphore;
semop(semid,&vsembuf,1);
return;
}
答案 0 :(得分:2)
使用
semid = semget(SEMKEY, NUM_SEMS, IPC_CREAT|0777
。
如果信号量不存在,它将创建信号量。