Unix system calls : read/write and the buffer

brucebanner

I am writing a pretty simple script .

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


int main(){
        int pipefd[2];
        pid_t c;
        int value[2];
        c = fork();
        if(c<0){
                perror("in fork");
                exit(1);
        }
        if(c==0){
                printf("i am the child\n");
                int buf[2];
                buf[0]=3;
                buf[1]=0;
                write(pipefd[1], buf, 4);
                write(pipefd[1],buf+1,4);
                close(pipefd[1]);
                exit(0);
        }
          if (pipe(pipefd) == -1) {      /*UPDATE */
                        perror("pipe");   
                        exit(EXIT_FAILURE);
          }

        read(pipefd[0], value, 4);
        read(pipefd[0], value+1, 4);
        close(pipefd[0]);
        printf("%d %d\n", value[0], value[1]);
        exit(0);
}

What I intend to do is to achieve:

value[0] = buf[0]; value[1] = buf[1];

( and print those of course).

But all I get as a result is :

-1299582208 32766
i am the child

Because, I have ints, I assumed that each will hold 4 bytes. And I think that for an int array each element will holds 4 bytes. But clearly I am missing something. Any help?

Craig Estey

As I mentioned in my top comment: Where is the pipe syscall?

Without it, the write and read calls will probably fail because pipefd has random values.

So, the parent will never have value filled in correctly.

Because these [unitialized] values are on the stack, they will have random values, which is what you're seeing.

This is UB [undefined behavior].

Different systems/compilers may manipulate the stack differently, which is why you see different [yet still random] results on different configurations.

To fix, add the following above your fork call:

pipe(pipefd);

I downloaded, built, and ran your program. Before I added the fix, I got random values. After applying the fix, I get 3 0 as the output, which is what you expected/wanted.

Note: As others have mentioned, you could check the return codes for read and write. If you had, they might return -1 and put an error code in errno that would have helped you debug the issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implicit system calls in UNIX commands

From Dev

How to list system calls and library calls in Unix?

From Dev

What does the (2) after Unix system calls names mean?

From Dev

Can MinGW replicate most unix system calls with no effort?

From Dev

should you use fread/fwrite or read/write system calls when you want to specify the buffer size explicitly?

From Dev

How to properly pass buffer pointers to Linux system calls in x86_64 assembly?

From Dev

how to print first 10 lines of a text file using Unix system calls?

From Java

What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

From Dev

Basics questions regarding File and I/O System Calls in C (on Linux/UNIX)

From Dev

exec() and system() system calls

From Dev

Misunderstand line-buffer in Unix

From Java

What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64

From Java

intercepting file system system calls

From Dev

API with no system calls

From Dev

Need of some system calls

From Dev

Concurrent system calls in Linux

From Dev

Is it safe to restart system calls?

From Dev

Can system calls be interrupted?

From Dev

Linux:System calls for Who

From Dev

What are unimplemented system calls?

From Dev

Making System Calls with Options

From Dev

System Calls in OS/161

From Dev

multithreading system calls in python

From Dev

List of system calls with explanation?

From Dev

C system calls fails

From Dev

C system calls open()

From Dev

System Calls in Perl

From Dev

Storage buffer synchronisation between draw calls with vulkan

From Dev

Is direct buffer required for JNI / external library calls?

Related Related

HotTag

Archive