gpt4 book ai didi

C 调用 semget 返回错误 "semget: No such file or directory"

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:02 26 4
gpt4 key购买 nike

我是一个 C 菜鸟,正在尝试创建一个使用 semget 的程序(首先是 main.c 的 if 语句)。我在 linux 机器上工作并编写了一些涉及信号量的代码。当我运行我的程序时,它返回“semget:没有这样的文件或目录”。我没有包括 carbon.c 和 hydrogen.c 文件,但如果有人认为他们对问题有必要,我会把它们放上来。感谢您的帮助。

主.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
}

主.h

#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;
}

最佳答案

使用

semid = semget(SEMKEY, NUM_SEMS, IPC_CREAT|0777

如果不存在,它将创建信号量。

关于C 调用 semget 返回错误 "semget: No such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com