C 进程间通信分叉()-计时等待()和/或睡眠()
我被要求将使用者(客户机)端开发为生产者(服务器),生产者在其中创建进程,等待消费者读取共享内存并删除进程,然后将控制权传递回生产者,以终止进程并关闭共享内存块 我研究了sleep和wait之间的区别,并意识到一旦调用fork(),子进程就开始运行 下面的代码是创建进程之后的代码,并检查它们是否是父进程。如果是,则等待(0)*现在我的问题是,我如何知道使用者中的代码从哪里开始执行,以及如何将其传回*C 进程间通信分叉()-计时等待()和/或睡眠(),c,unix,fork,ipc,wait,C,Unix,Fork,Ipc,Wait,我被要求将使用者(客户机)端开发为生产者(服务器),生产者在其中创建进程,等待消费者读取共享内存并删除进程,然后将控制权传递回生产者,以终止进程并关闭共享内存块 我研究了sleep和wait之间的区别,并意识到一旦调用fork(),子进程就开始运行 下面的代码是创建进程之后的代码,并检查它们是否是父进程。如果是,则等待(0)*现在我的问题是,我如何知道使用者中的代码从哪里开始执行,以及如何将其传回* else if(pid > 0) {
else if(pid > 0)
{
wait(0);
}
下面可以看到生产者使用的主循环
int noToCreate = atoi(argv[2]); // (user inputs on cmd line "./prod 20 10 5" - 20 size of shared mem, 10 process to be created, 5 processes to be deleted)
while(*memSig != 2)
{
while(*memSig == 1) // set memsignature to sleep while..
{
sleep(1);
}
for(B = 0; B < noToCreate; B++)
{
pid = fork();
if(pid == -1)
{
perror("Error forking");
exit(1);
}
else if(pid > 0)
{
wait(0);
}
else
{
srand(getpid());
while(x == 0)
{
if(*randNum == 101)
{
*randNum = rand() % (100 -
1) + 1;
*pidNum = getpid();
printf("priority: %d
Process ID: %d \n", *randNum, *pidNum);
x = 1;
}
else
{
*randNum++;
*pidNum++;
}
}
exit(0);
}
} /* Closes main for loop */
if(*memSig == 0)
{
*memSig = 1;
}
} /* Closes main while loop */
int noToCreate=atoi(argv[2]);//(用户输入cmd行“/prod 20 10 5”-20大小的共享内存,创建10个进程,删除5个进程)
而(*memSig!=2)
{
while(*memSig==1)//将memsignature设置为sleep while。。
{
睡眠(1);
}
对于(B=0;B<0;B++)
{
pid=fork();
如果(pid==-1)
{
perror(“错误分叉”);
出口(1);
}
否则,如果(pid>0)
{
等待(0);
}
其他的
{
srand(getpid());
而(x==0)
{
如果(*randNum==101)
{
*randNum=rand()%(100-
1) + 1;
*pidNum=getpid();
printf(“优先级:%d
进程ID:%d\n“,*randNum,*pidNum);
x=1;
}
其他的
{
*randNum++;
*pidNum++;
}
}
出口(0);
}
}/*关闭主回路*/
如果(*memSig==0)
{
*memSig=1;
}
}/*关闭主回路*/
感谢一帮家伙:)
等待
让家长在继续之前等待任何孩子终止(最好使用waitpid
等待某个孩子),而sleep
将进程置于睡眠状态,并在参数结束时立即恢复。两个调用都将使进程阻塞
而且不是说孩子会马上跑,这是不确定的行为 如果要在生产者和消费者之间传递数据,请使用管道或*NIX套接字,如果单个整数足够,则使用子级的返回值
exit
请参见,您可以使用宏WEXITSTATUS
获取子级的返回值
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid());
if (argc == 1)
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
} else { /* Code executed by parent */
do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
}
#包括
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
pid_t cpid,w;
智力状态;
cpid=fork();
如果(cpid==-1){
佩罗尔(“福克”);
退出(退出失败);
}
如果(cpid==0){/*子级执行的代码*/
printf(“子PID为%ld\n”,(长)getpid();
如果(argc==1)
暂停();/*等待信号*/
_出口(atoi(argv[1]);
}else{/*由父级执行的代码*/
做{
w=等待PID(cpid和状态,WUNTRACED | WCONTINUED);
如果(w==-1){
佩罗(“waitpid”);
退出(退出失败);
}
如果(妻子退出(状态)){
printf(“已退出,状态=%d\n”,WEXITSTATUS(状态));
}否则如果(WIFSIGNALED(状态)){
printf(“被信号%d\n杀死”,WTERMSIG(状态));
}否则如果(WIFSTOPED(状态)){
printf(“被信号%d\n停止”,WSTOPSIG(状态));
}否则如果(WIFCONTINUED(状态)){
printf(“续”);
}
}而(!WIFEXITED(status)和&!WIFSIGNALED(status));
退出(退出成功);
}
}
wait
使父进程在继续之前等待任何子进程终止(最好使用waitpid
等待某个子进程),而sleep
将进程置于睡眠状态,并在参数结束时立即恢复进程。
两个调用都将使进程阻塞
而且不是说孩子会马上跑,这是不确定的行为
如果要在生产者和消费者之间传递数据,请使用管道或*NIX套接字,如果单个整数足够,则使用子级的返回值exit
请参见,您可以使用宏WEXITSTATUS
获取子级的返回值
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid());
if (argc == 1)
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
} else { /* Code executed by parent */
do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
}
#包括
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
pid_t cpid,w;
智力状态;
cpid=fork();
如果(cpid==-1){
佩罗尔(“福克”);
退出(退出失败);
}
如果(cpid==0){/*子级执行的代码*/
printf(“子PID为%ld\n”,(长)getpid();
如果(argc==1)
暂停();/*等待信号*/
_出口(atoi(argv[1]);
}else{/*由父级执行的代码*/
做{
w=等待PID(cpid和状态,WUNTRACED | WCONTINUED);
如果(w==-1){
佩罗(“waitpid”);
退出(退出失败);
}
如果(妻子退出(状态)){
printf(“已退出,状态=%d\n”,WEXITSTATUS(状态));
}否则如果(WIFSIGNALED(状态)){
printf(“被信号%d\n杀死”,WTERMSIG(状态));
}否则如果(WIFSTOPED(状态)){
printf(“被信号%d\n停止”,WSTOPSIG(状态));
}否则如果(WIFCONTINUED(状态)){
printf(“续”);