Thread: C Programming in UNIX

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    C Programming in UNIX

    I have the code which uses two pipes , and it passes the info to child process which converts the input string to uppercase , which is returned to parent and parent displays the new string

    ISSUE : It only read one word and terminates as soon as reaches a first space/null
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    #include <ctype.h>
    //need two pipes and string input 
    
    
    
    
    int main (int argc, char *argv[])
    {
        int pipe1[2];
        int pipe2[2];
        char input[100];
        
        if(pipe(pipe1) == -1) { // if any issues creating the pipe
        printf("pipe1 failed\n");
        exit(1);}
        
        if(pipe(pipe2) == -1) { // if any issues creating the pipe
        printf("pipe2 failed\n");
        exit(1);}
        
        scanf("%s", input);
        if(fork() < 0){
        printf("fork failed\n");
        }
        else if(fork() ==0){ //that is child process to convert string to uppercase
        char new[100] ; //to save the uppercase string
        //child only read so close writing end
        close (pipe1[1]);
        read(pipe1[0],new, 100);
        int l = strlen(new);
        for (int i = 0; i < l ; i++){
        new[i] = toupper(new[i]);
        }
        close(pipe1[0]);
        close(pipe2[0]);
        write(pipe2[1],new, l+1);  //length of the string 
        close(pipe2[1]);
        exit(0);
        }else (fork() >0);{
        char new[100];
        close(pipe1[0]);
        
        write(pipe1[1], input, strlen(input)+1);
        close(pipe1[1]);
        
        //once that is done child process starts so 
        wait(NULL);
        close(pipe2[1]);
        read(pipe2[0],new, 100);
        printf("new string %s\n", new);
        close(pipe2[0]);
        
        }
        
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are several problems.

    1. Your code looks like dog-food. The indentation is awful, and probably hiding your mistakes.

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    #include <ctype.h>
    //need two pipes and string input 
    
    int main(int argc, char *argv[])
    {
      int pipe1[2];
      int pipe2[2];
      char input[100];
    
      if (pipe(pipe1) == -1) {      // if any issues creating the pipe
        printf("pipe1 failed\n");
        exit(1);
      }
    
      if (pipe(pipe2) == -1) {      // if any issues creating the pipe
        printf("pipe2 failed\n");
        exit(1);
      }
    
      scanf("%s", input);
      if (fork() < 0) {
        printf("fork failed\n");
      } else if (fork() == 0) {     //that is child process to convert string to uppercase
        char new[100];              //to save the uppercase string
        //child only read so close writing end
        close(pipe1[1]);
        read(pipe1[0], new, 100);
        int l = strlen(new);
        for (int i = 0; i < l; i++) {
          new[i] = toupper(new[i]);
        }
        close(pipe1[0]);
        close(pipe2[0]);
        write(pipe2[1], new, l + 1);  //length of the string 
        close(pipe2[1]);
        exit(0);
      } else
        (fork() > 0);   //!! WTF is this!?
        {  
        char new[100];
        close(pipe1[0]);
    
        write(pipe1[1], input, strlen(input) + 1);
        close(pipe1[1]);
    
        //once that is done child process starts so 
        wait(NULL);
        close(pipe2[1]);
        read(pipe2[0], new, 100);
        printf("new string %s\n", new);
        close(pipe2[0]);
        }
    }
    2. You're creating more processes than you bargain for.
    Code:
      if (fork() < 0) {
        printf("fork failed\n");
      } else if (fork() == 0) {     //that is child process to convert string to uppercase
    NO!!!
    This does not create one child and tests whether it's parent or child, it creates two children

    Code:
    pid_t p = fork();
    if ( p < 0 } {
    } else if ( p == 0 ) {
    } else {
    }
    3. Line 46 -> (fork() > 0); //!! WTF is this!?
    else clauses don't have conditions.

    4. ISSUE : It only read one word and terminates as soon as reaches a first space/null
    Well use fgets to read a whole line, instead of scanf %s, which stops at the first white space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming in UNIX
    By PRJ in forum C Programming
    Replies: 7
    Last Post: 11-26-2019, 06:52 AM
  2. programming in c on unix
    By roaan in forum C Programming
    Replies: 8
    Last Post: 08-19-2009, 08:06 AM
  3. Unix programming & CLA
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2008, 10:22 AM
  4. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 08:44 PM
  5. UNIX and C programming
    By atif in forum Linux Programming
    Replies: 0
    Last Post: 06-15-2002, 06:12 PM

Tags for this Thread