Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97355

Why are the terminal and VSCode-Code Runner's results differerent?

$
0
0

I wrote a toy project related to the concept of processes in the operating system and ran it respectively with the same command in VSCode-Code-Runner and terminal. I found that the results are surprisingly different. The Code Runner output has one more line than the terminal's output (which is not expected).

Screenshots:

enter image description here

enter image description here

Code:

#include <cstdlib>
//#include<cstdio>
#include <unistd.h>
#include <cstring>
#include <fcntl.h>
#include <cassert>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char*argv[]){
    char buf;
    int fd[2];
    if(pipe(fd)<0){
        cerr<<"pipe failed\n";
        exit(1);
    }
    char *p;
    p = (char *)malloc(12 * (sizeof(char *)));
    int rc1=fork();
    if(rc1<0)
    {
        // fork failed; exit
        cerr << "fork failed\n";
        exit(1);
    }
    else if(rc1==0){
        printf("hello, I am child (pid:%d)\n", (int)getpid());
        close(fd[0]);
        write(fd[1], "hello world\n", 12);
        return 0; //否则会继续fork一个进程
    }
    else if(rc1>0){
        printf("hello, I am parent of %d  (pid:%d)\n",
               rc1, (int)getpid());
    }
    wait(NULL);
    int rc2 = fork();
    if (rc2 < 0)
    {
        // fork failed; exit
        cerr << "fork failed\n";
        exit(1);
    }
    else if (rc2 == 0)
    {
        close(fd[1]);
        write(STDOUT_FILENO, "\n", 1);
        while (read(fd[0], &buf, 1) > 0)
        {
            write(STDOUT_FILENO, &buf, 1);
        }
        write(STDOUT_FILENO,"\n", 1);

        printf("hello, I am child (pid:%d)\n", (int)getpid());
        return 0;
    }
    else if (rc2 > 0)
    {
        printf("hello, I am parent of %d  (pid:%d)\n",
               rc2, (int)getpid());
    }
    return 0;
}

Viewing all articles
Browse latest Browse all 97355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>