获得免费:执行代码时大小无效

问题描述

使用 gcc hw2.c -o x -lpthread 编译

free(): invalid size
free(): invalid size
free(): invalid size
nano infile.txt

我假设它可能与文件指针有关?已注释掉信号量以首先解决此问题。所有答案都指向指针,但切换事物并没有多大帮助。我试过以不同的方式编译,但也无济于事。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/stat.h>

//sem_t X;

void process(){
    //sem_open("X",O_CREAT,0777,0);
    int ret;
    int N = 1;
    pid_t pid;
    FILE* infile = fopen ("infile.txt","r");

    for(int i = 0; i< 50; i++){
        fscanf (infile,"%d",&N);
        fclose (infile);
        printf("N: %d Process ID: %d",N,pid);
        infile = fopen("infile.txt","w");
        N++;
        //sem_post(&X);
        fprintf(infile,N);
        fflush(infile);
        fclose(infile);
    }
    printf("\n");
    int c;
    //sem_getvalue(&X,&c);
    printf(" \n \n \n%d",c);
}
int main(){
    int pid,pid1,pid2;
    pid = fork();
    if(pid == 0){
        //child1,Last
        printf("Starting Process C: ");
        process();
    }
    else{
        pid1 = fork();
        if(pid1 == 0){
            //child2,Middle
            printf("Starting Process B: ");
            process();
        }
        else{
            pid2 = fork();
            if(pid2 == 0){
                //child 3,First
                printf("Starting Process A: ");
                process();
            }
            else{
            }
        }
        
    }
    //sem_close(&X);
    //sem_unlink(&X);
}

解决方法

你的循环有问题

FILE* infile = fopen ("infile.txt","r"); //1

for(int i = 0; i< 50; i++){
    fscanf (infile,"%d",&N);
    fclose (infile); //2
    printf("N: %d Process ID: %d",N,pid);
    infile = fopen("infile.txt","w"); //3
    N++;
    //sem_post(&X);
    fprintf(infile,N);
    fflush(infile);
    fclose(infile); //4
}

您在 1 打开文件,然后进入循环,在 2 关闭它,在 3 重新打开它并在 4 重新关闭它。在下一次迭代中,当您尝试在 2 关闭时,您将遇到双重释放,因为它已经4 关闭。

,

最明显的问题是在你的循环中。我已经删除了除 fopenfclose 调用之外的所有内容:

FILE* infile = fopen ("infile.txt","r");

for(int i = 0; i< 50; i++){
    fclose (infile);
    infile = fopen("infile.txt","w");
    fclose(infile);
}

正如您现在可能看到的,当 i1 时,您尝试 fclose(infile) - 但它没有打开,因此会出现错误。

您需要将第一个 fopen 移动到循环中 - 并检查打开文件和读取文件是否也成功:

#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

void process() {
    int N = 1;
    for(int i = 0; i < 50; i++) {
        FILE* infile = fopen("infile.txt","r");
        if(infile) {
            bool readok = fscanf(infile,&N) == 1;
            fclose(infile);

            if(readok) { /* only do this if a value was read from the file ok */
                printf("N: %d Process ID: %d",getpid());
                infile = fopen("infile.txt","w");
                if(infile) {
                    N++;
                    fprintf(infile,N);
                    fflush(infile);
                    fclose(infile);
                }
            }
        }
    }
    printf("\n");

    exit(0); /* terminate this sub process */
}

int main() {
    const size_t kPids = 3;

    pid_t pids[kPids]; /* simplify keeping a number of background processes */

    for(size_t i = 0; i < kPids; ++i) {
        pids[i] = fork();
        if(pids[i] == 0) {
            printf("Starting Process %c:\n",(char)('A' + i));
            process();
        }
    }

    /* wait for children to finish */
    pid_t pid;
    int wstatus;
    while((pid = wait(&wstatus)) != -1) {
        printf("pid %d is done with status %d\n",pid,wstatus);
    }
}