我必须将N个客户端进程与一个服务器同步.这些进程由一个main函数分叉,我在其中声明了3个信号量.我决定使用POSIX信号量,但我不知道如何在这些进程之间共享它们.我认为共享内存应该正常工作,但我有一些问题:
sizeof(sem_t)在size_t字段中shmget使用以便准确分配我需要的空间吗?当我尝试执行这部分代码时,我得到一个"shmget:无效的参数错误"
int *nFS, *spb, *cell1, shmid;
key_t key = 5768;
//i need a shared memory segment in which i can put 3 ints
if ((shmid = shmget(key, (sizeof(int) * 3), IPC_CREAT | 0666)) < 0 ) {
perror("shmget");
exit(1);
}
if ((spb = (int)shmat(shmid, NULL, 0))== -1 ){
perror("shmat");
exit(1);
}
cell1= spb + 1 ;
nFS= cell1 + 1;
//i try to assign here 7 to nFS
*nFS=7;
Run Code Online (Sandbox Code Playgroud)
这里有问题,但我无法弄清楚是什么.你能帮助我吗?
谢谢,亚历克斯.
我必须向进程发送两个信号,SIGUSR1并且SIGUSR2为了修改程序中的特定布尔变量(SIGUSR1将其设置为true,SIGUSR2将其设置为false).所以我写了一个signalHandler()函数来控制SIGUSR1或的行为SIGUSR2.问题是:如何设置sigaction()处理这个特定的任务? 我花了很多时间在谷歌上,我到处读到我应该使用sigaction()而不是过时的signal().在手册页中我找到了这个
int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact);
Run Code Online (Sandbox Code Playgroud)
在signum我必须把我想要处理的信号类型,然后我有一个struct sigaction参数:
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
Run Code Online (Sandbox Code Playgroud)
在第一个字段中我认为我应该设置我的信号处理程序的名称,但我不知道如何设置其他字段.
最后,有什么用struct sigaction *oldact?:
我有这个代码;
pid_t process;
process = fork();
if (process < 0){
//fork error
perror("fork");
exit(EXIT_FAILURE);
}
if (process == 0){
//i try here the execl
execl ("process.c", "process" , n, NULL);
}
else {
wait(NULL);
}
Run Code Online (Sandbox Code Playgroud)
我不知道这种使用fork()和exec()组合是否正确.当我尝试从bash运行程序时,我没有收到任何结果,所以我认为这部分代码可能存在问题.
谢谢.