-3

I am trying to write a simple program which passes the PID of the parent process to the child process and print, however when I use the atoi() function I am not receiving the correct parent PID value. I'm not sure what I am doing wrong. I have read that the atoi() function is ASCII to integer, which makes me wonder if this is the right function. I have read that the command line arguments must be passed as strings. I have just begun learning C, so apologies if this seems like a silly question. I have provide both .c files below.

This is the Parent Process

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

#define COUNT 1

int pfd[2];

int main()
{
    pid_t process_id;

    char pipeReadstr[11];
    char str1_pid[10];

    // Set up the pipe
    pipe(pfd)
    
    sprintf(str1_pid, "%d", getpid());

    //Set up the read file descriptor
    sprintf (pipeReadstr, "%d", pfd[0]);

    process_id = fork();

    if (process_id == 0)
    {
        close(pfd[1]);//Close the write end of pipe 0
        execl("../receiveVar/receiveVar.exe", "receiveVar", pipeReadstr, 
              (char *)NULL);
    }
    sleep(2);

    // Passing str1 to the write end of pipe 0, then close write and read ends
    write(pfd[1], str1_pid, strlen(str1_pid));
    close(pfd[1]);
    close(pfd[0]);
}

This is the Child Process

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

#define READ_BLOCK_SIZE 10

int main(int argc, char *argv[])
{
    pid_t str = getpid();

    char readBuffer[READ_BLOCK_SIZE+1];
    ssize_t bytesRead;

    int readFd;

    readFd = atoi(argv[1]);
    bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);

    while (bytesRead > 0)
    {
        readBuffer[bytesRead] = '\0';
        printf("Child Process: Received parent process PID: %d. Child process read %li bytes.\n", readFd, bytesRead);
        printf("Child Process PID: %d\n", str);
        bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
    }
    close(readFd);
}
Dan
  • 11
  • 2
  • 3
    There is an issue with your code which can be seen by just looking at the syntax highlighting. If your question is about `atoi` then you should reduce your code to a [mcve] focusing on the subject. – Eugene Sh. Sep 15 '22 at 14:49
  • 3
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 15 '22 at 14:54
  • Hi Eugene Sh., I have fixed up the error and reduced lines of code. I'm worried if I remove any more people may not get a good sense of what I have tried. Please let me know If I should further reduce the code. – Dan Sep 15 '22 at 15:18
  • You only do `sprintf(str1_pid, "%d", getpid());` when `pipe(pfd)` returns less than 0, which means it failed. If `pipe()` fails you should report the error and exit. – Barmar Sep 15 '22 at 15:39
  • Sorry Barmar, I was trying to remove a lot of code for readability and that part was in an if statement reporting an error as you mentioned. I will add it back. – Dan Sep 15 '22 at 15:54
  • [`atoi()` is a really bad function to use for processing input - it has no way to return any error](https://stackoverflow.com/questions/1488569/atoi-string-to-int). Every value `atoi()` can return is a valid result from input. [And it gets worse](https://port70.net/~nsz/c/c11/n1570.html#7.22.1p1): "If the value of the result cannot be represented, the behavior is undefined." – Andrew Henle Sep 15 '22 at 23:34

1 Answers1

1

I have managed to see where I had my error and now the code works fine. I passed the incorrect variable which also meant I had the wrong specifier.

while (bytesRead > 0)
{
    readBuffer[bytesRead] = '\0';
    printf("Child Process: Received parent process PID: %s. Child process read 
    %li bytes.\n", readBuffer, bytesRead);
    printf("Child Process PID: %d\n", str);
    bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
}
close(readFd);
Dan
  • 11
  • 2