使用fork调用后如何在进程之间传递值?

如何解决使用fork调用后如何在进程之间传递值?

我需要创建一个程序,该程序将开始4个进程之间的竞赛,以从用户输入中获取数字。用户将输入4个数字(1到4),接收第一个数字(1)的过程将是赢家,而在第一位,其后的过程将获得第二,第三和第四位,具体取决于他们从用户。

该程序将通过使用fork()系统调用来创建4个进程,每个创建的进程由1到4的数字标识,该数字指示其创建顺序。例如,创建的第一个子进程的get ID = 1。

毕竟创建了进程,这些进程将开始向用户请求一个号码。每个进程将打印从用户那里获得的号码,并打印其ID号。父级不参加比赛,而是等待所有子级进程完成后退出。

我认为我已经完成了基本结构,但是我不确定如何在流程之间传递值,或者不确定是否正在实现合法的竞争条件。我也无法运行最后3个进程。

这是我的输出:

My output

预期输出:

Expected Output

这是我到目前为止所拥有的:


#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

    int p1,p2,num; 

int procNUM = 1,runNum = 1 ;

void game(int p1,int p2);

int main() {
    
     printf("Enter numbers for processes to race for >>>\n");

    p1 = fork();

    p2 = fork();
    
if (p1 > 0 && p2 > 0) { 
       //parent 
       
      
            printf(" I am a process with pid %d and I am process number %d in the race.\n",getpid(),procNUM);
            procNUM++;
           // fflush(stdout);
           // scanf("%d",&num);
           // printf("%d",&num);
           // printf(I am a processs with);
       
    } 
    
    else if (p1 == 0 && p2 > 0) 
    { 
    //child 1 
    
     printf(" I am a process with pid %d and I am process number %d in the race.\n",procNUM);
     procNUM++;
    // fflush(stdout);
     
    } 
    
    else if (p1 > 0 && p2 == 0) 
    { 
    //child 2 
   
    printf(" I am a process with pid %d and I am process number %d in the race.\n",procNUM);
    procNUM++;
    //fflush(stdout);
    
    
    } 
    
    else { 
       //child 3
       
    printf(" I am a process with pid %d and I am process number %d in the race.\n",procNUM);
    procNUM++;
   // fflush(stdout);
   
       
        
    } 


    game(p1,p2);

 return 0;   
}


void game(int p1,int p2){
    if (p1 > 0 && p2 > 0) { 
       //parent 
            scanf("%d",&num);
            printf(" I am a process with pid %d and I am process number %d in the race and I am in %d place.\n",procNUM,runNum);
            //fflush(stdout);
            runNum++;
       
    } 
    
    else if (p1 == 0 && p2 > 0) 
    { 
    //child 1 
    
            scanf("%d",runNum);
            //fflush(stdout);
       runNum++;
    
    
    } 
    
    else if (p1 > 0 && p2 == 0) 
    { 
    //child 2 
   
            scanf("%d",runNum);
           // fflush(stdout);
       runNum++;
    
    } 
    
    else { 
       //child 3
       
            scanf("%d",runNum);
            //fflush(stdout);
       runNum++;
        
    } 
    
    
    
}





解决方法

使用fork后,您的procNUM变量会“分支”,并且与父级的procNUM没有任何共同之处,因此增加它不会告诉父级进程任何内容(写时复制)。

要将值传递给子进程,请使用argv []数组-它是execvp函数的参数。

我建议采用标准方式:在主程序中启动多个线程,这些线程中的每个线程都会启动一个子进程。子进程(game.c)代码很简单:

#include <stdio.h>
int main(int argc,char** argv)
{
    int num;
    scanf("%d",&num);
    return num;
}

父程序涉及更多。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// #include <threads.h>
// #include <stdatomic.h>
#include <pthread.h>

// Compile the program above to  'game' using "gcc -o game game.c"
#define N (4)
const char* game_file = "./game";

// Declare PID array,ret_code and threads
int pids[N];
int ret_code[N];
pthread_t thread[N];
// C11: thrd_t thread[N];

// Each thread increments this atomic int when the child process finishes
/* C11: atomic_int */ int proc_finish_count;

线程过程是

int game_proc(void *arg) {
    int i = *((int*)arg);
    pids[i] = fork();
    if (pids[i] > 0) {
        while(1) {
            int st;
            int res = waitpid(pids[i],&st,WNOHANG);
            if (res == pids[i]) {
                proc_finish_count++;
                // In C11 use atomics :
                // atomic_fetch_add_explicit(&proc_finish_count,1,memory_order_relaxed);
                printf(" I am a process with pid %d and I am process number %d in the race and I am in %d place.\n",pids[i],i,proc_finish_count);
                break;
            } else
            if (res < -1) {
                // error while waiting
                return 1;
            }
        }
    } else { 
        // Here you can pass parameters to `game` - insert the list of strings before NULL
        execlp(game_file,game_file,NULL);
        printf("Error running process\n");
    } 

    return 0;
}

main函数只是启动并等待线程

int main(){
    int i;
    proc_finish_count = 0;
    for (i = 0 ; i < N ; i++) {
        ret_code[i] = i; // index of the process
        pthread_create(&thread[i],NULL,&game_proc,&ret_code[i]);
        // C11: thrd_create(&thread[i],&ret_code[i]);
    }

    for (i = 0 ; i < N ; i++)
        pthread_join(&thread[i],NULL);
        // C11: thrd_join(thread[i],NULL);

    return 0;
}

由于您的子进程为标准输入处理“竞争”,所以我建议启动像“ xterm”或其他终端仿真程序之类的子进程-这样,输入可以真正“竞争”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-