简体   繁体   English

执行期间段故障 11

[英]Seg Fault 11 during execution

I don't understand why I'm getting a segfault, I haven't used any pointers.我不明白为什么会出现段错误,我没有使用任何指针。 What I'm trying to do is Send messages between parent and child processes using two pipes, To do this I first fork then close/open required pipe sections and use the make_message to create the message I want to send.我想要做的是使用两个管道在父进程和子进程之间发送消息,为此我首先分叉然后关闭/打开所需的管道部分并使用 make_message 创建我想要发送的消息。 The problem might be that I first write with the child process and read with the parent process.问题可能是我先用子进程写,然后用父进程读。

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>

#define READ_END 0
#define WRITE_END 1
#define BUF_SIZE 256
void make_message (int num, char *message){
    char buftime[26];
    time_t timer;
    struct tm* tm_info;
    time (&timer);
    tm_info = localtime (&timer);
    strftime (buftime, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    sprintf (message, "%s %d at %s\n", "Hello I'm child number", num, buftime);
}

this is the make_message function这是 make_message 函数

int main (int agrc, char *argv[]){
    int p1[2];
    int p2[2];
    char *virtualaddr;
    
    if (pipe(p1) == -1){
        fprintf (stderr, "pipe failed");
        return 1;
    }
    
    if (pipe(p2) == -1){
        fprintf (stderr, "pipe failed");
        return 1;
    }
    
    close (p1[READ_END]);
    close (p2[WRITE_END]);
    close (p2[READ_END]);
    close (p1[WRITE_END]);
    
    switch(fork()){
    case -1:
        fprintf (stderr, "fork error");
        return 1;
    
    case 0: //pere mais (fils 1)
    {
        char buf[BUF_SIZE];
        
        do{
            char message[BUF_SIZE];
            //memset (message, 0, BUF_SIZE);
            /*-------------WRITE 1--------------*/
            close (p1[READ_END]); //ferme la partie non utilisé du pipe
            close (p2[WRITE_END]); //ferme la partie non utilisé du pipe
            printf ("1");
            make_message (1, message);
            strcpy (virtualaddr, message);
            //ecrit au pipe
            write (p1[WRITE_END], virtualaddr, BUF_SIZE);
            //ferme le write du pipe
            close (p1[WRITE_END]);
            /*-------------WRITE 1--------------*/
        
            sleep(1);
        
            /*-------------READ 4---------------*/
            read (p2[READ_END], buf, BUF_SIZE); //fils lit
            printf ("Message received by child 1 : %s\n", buf);
            fflush( stdout);
        
            //ferme read dans le pipe
            close (p2[READ_END]);
            /*-------------READ 4---------------*/
            
            }while(1);
            _exit(0);
        }break;
    default: { //fils 2
        char buf[BUF_SIZE];
        //char message[BUF_SIZE];
        
        do{
            char message[BUF_SIZE];
            //memset (message, 0, BUF_SIZE);
            /*-------------READ 2---------------*/
            close (p2[READ_END]);
            close (p1[WRITE_END]); //ferme partie non utilisé du pipe
            printf("2");
            read (p1[READ_END], buf, BUF_SIZE); //fils lit
            printf ("Message received by child 2 : %s\n", buf);
            fflush( stdout);
        
            //ferme read dans le pipe
            close (p1[READ_END]);
            /*-------------READ 2---------------*/
        
        
            /*-------------WRITE 3--------------*/
            make_message (2, message);
            strcpy (virtualaddr, message);
            //ecrit au pipe
            write (p2[WRITE_END], virtualaddr, BUF_SIZE);
            //printf ("%s", buf);
            fflush( stdout);
        
            //ferme le write du pipe
            close (p2[WRITE_END]);
            /*-------------WRITE 3--------------*/
        
            memset (buf, 0, sizeof(buf));
            sleep(1);
        }while(1);
        _exit(0);
    }
    break;
}
        
}
    

I haven't used any pointers.我没有使用任何指针。

Yes you have:是的,你有:

char *virtualaddr;

And it is never initialized before you do在你做之前它永远不会被初始化

strcpy (virtualaddr, message);

Compile with -Wall -O and I bet you get a warning about this.-Wall -O编译,我敢打赌你会收到关于这个的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM