c - 进程不写入文件?

标签 c bash file-handling minix

,这是一个相当长的问题,所以请耐心等待。我想做的是同时运行两个进程。每个进程都会读取文件 foo.txt 找到最后一个数字,将其递增并将其添加回文件中。由于会有明显的竞争条件,我正在尝试实现彼得森的解决方案来避免它。

这一切都是在 Minix3 环境中完成的。我已经定义了一个变量 shared_val ,它针对系统上运行的每个进程进行初始化。还有两个系统调用,get_sv 返回shared_val 的值,set_sv 将用户值设置为shared_val。

我使用每个进程的shared_val 值来实现Peterson 的解决方案。我还将这两个进程 ID 写入 .txt 文件 config.txt

代码可以正常编译,但不会写入 foo.txt。有人可以解释为什么会发生这种情况吗?

#include <stdio.h>
#include <stdlib.h>
#include "sys/types.h"
#include <unistd.h>
#include <strings.h>
#define MAX 10000

int main(int argc, char *argv[])
{
    int yourPID, status = 0, tempid, temp, temp1, i = 0, times;
    int ch[10];
    int a, b, tempo, x, y;
    FILE *fp, *fp1;
    times = atoi((argv[1]));
    ch[i++] = getpid();
    fp = fopen(argv[3], "a");
    while(i>=0)
    {
        fprintf(fp, "%d", ch[1]);
        i--;
    }
    fclose(fp);
    if(yourPID == ch[0])
    {
        set_sv(0, &status);
    }
    if(yourPID == ch[1])
    {
        set_sv(0, &status);
    }
    do 
    {
        yourPID = getpid();
        if(yourPID == ch[0])
        {
            temp = get_sv(ch[0], &status);
        }
        if(yourPID == ch[1])
        {
            temp1 = get_sv(ch[1], &status);
        }
        sleep(1);
        a = ~temp & ~temp1;
        b = temp & temp1;
        sleep(1);
        if(yourPID == ch[0] && ((~a & ~b) == 0))
        {
            char ch1[MAX], len, pos;
            fp1 = fopen(argv[2], "r");
            while(!feof(fp))
            {
                fscanf(fp1,"%s", ch1);
            }
            fclose(fp1);
            len = strlen(ch1);
            pos = len - 1;
            tempo = ch1[pos] + 1;
            fp1 = fopen(argv[2], "a");
            fprintf(fp1, "%c", tempo);
            fclose(fp1);
            tempo = get_sv(yourPID, &status);
            if(tempo == 0)
            {
                tempo = 1;
                set_sv(tempo, &status);
            }
            if(tempo == 1)
            {
                tempo = 0;
                set_sv(tempo, &status);
            }
            sleep(1);
            continue;
        }
        if(yourPID == ch[1] && ((~a & ~b) == 1))
        {
            char ch1[MAX], len, pos;
            fp1 = fopen(argv[2], "r");
            while(!feof(fp1))
            {
                fscanf(fp1, "%s", ch1);
            }
            fclose(fp1);
            len = strlen(ch1);
            pos = len - 1;
            tempo = ch[pos] + 1;
            fp1 = fopen(argv[2], "a");
            fprintf(fp1, "%c", tempo);
            fclose(fp1);
            tempo = get_sv(yourPID, &status);
            if(tempo == 1)
            {
                tempo = 0;
                set_sv(tempo, &Status);
            }
            else
            {
                tempo = 1;
                set_sv(tempo, &status);
            }
            sleep(1);
            continue;
        }
    times = times - 1;
    }while(times > 0);
    return 0;
}

我在整个代码中添加了 sleep(1) 语句,以便让其他进程有时间 catch 。

我为此使用的 bash 命令是:./safe_increment 5 foo.txt config.txt & ./safe_increment 5 foo.txt config.txt

其中 5 是每个进程写入文件的次数。

最佳答案

这甚至不应该编译——声明 yourPID 的第一行在状态初始值设定项之后有错误,并且我没有看到 i 的声明。这可能是剪切粘贴错误,但后续有很多事情不清楚您要做什么,或者根本不起作用。

一般来说,仅 sleep() 不会为您提供可靠的进程间同步。

关于c - 进程不写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252326/

相关文章:

c - 如何使用包含 swi-prolog.h 的 gcc 创建 .dll 文件?

在 C 中创建 BMP 文件(位图)

python - 无法使用 child.before 获取数据

regex - SunOS - (Bash) Grep 命令从不包含模式的匹配中获取行

linux - 为 Linux 构建脚本时出错

java - 在文件处理中如何从 char[] 数组读取文件?

c - C 宏的负面污名

c - 一元 '*' 的无效类型参数(具有 'int')C 中的错误

c - 从重定向为输入的文件中验证日期

c - 检测阻塞打开的 FIFO 文件的删除