problem implementing fork

This is a discussion on problem implementing fork within the C Programming forums, part of the General Programming Boards category; Hi, I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    problem implementing fork

    Hi,
    I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty file creator program. While creating a large file it must print # for progress bar. But the output shows it happening reverse way. ie. first it copies file and shows the progress bar(although the bar is filled completely thus showing that parent process is working correctly). Kindly help me in putting these progress # simultaneously to file copying. Thanks in advance.
    Code:
    Code:
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    static int status = 0;
    void ding()
    {
    status =1;
    }
    int main(int argc, char* argv[])
    {
    int rc=0,fd,i,tem=0;
    char c=' ';
    int size= atoi(argv[1])*1024;
    char *add=argv[2];
    
    pid_t pid;
    pid = fork();
    switch(pid){
    case -1:
    perror("Error creating fork\n");
    exit(1);
    
    case 0:
    /*child process*/
    {
    fd = open(add,O_WRONLY|O_CREAT,0666);
    if (fd<0)
    {
    printf("Error creating file\n");
    exit(1);
    }
    for(i=1;i<=size;i++)
    {
    tem = write(fd, &c, 1);
    rc += tem;
    }
    printf("rc: %d\n",rc);
    if(rc!=size)
    perror("Error allocating size.\n");
    else
    kill(getppid(),SIGALRM);
    
    close(fd);
    exit(0);
    }
    /*child process ends*/
    }
    /*parent process*/
    (void) signal(SIGALRM,ding);
    while(1){
    printf("#");
    sleep(1);
    if(status)
    {
    printf("\ndone.\n");
    exit(1);
    }
    }
    }
    Output:
    Code:
    dheeraj@dheeraj-machine:~/linux_pro$ ./a.out 1000 temp
    hangs for some time. then:
    Code:
    rc: 1024000
    ###########################
    done.
    Clearly these # should have been printed along with file copy process. But don't know why its not.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while(1){
        printf("#");
        fflush( stdout );
    Try something like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Thumbs up

    Quote Originally Posted by quzah View Post
    Code:
    while(1){
        printf("#");
        fflush( stdout );
    Try something like that.


    Quzah.
    Thanks quzah. Worked like charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21