C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-24-2009, 05:09 PM   #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.
dheerajsuthar is offline   Reply With Quote
Old 09-24-2009, 05:28 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
Code:
while(1){
    printf("#");
    fflush( stdout );
Try something like that.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 09-24-2009, 11:09 PM   #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.
dheerajsuthar is offline   Reply With Quote
Reply

Tags
fork, linux, system call

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM
searching problem DaMenge C Programming 9 09-12-2005 01:04 AM
half ADT (nested struct) problem... CyC|OpS C Programming 1 10-26-2002 08:37 AM
binary tree problem - help needed sanju C Programming 4 10-16-2002 05:18 AM


All times are GMT -6. The time now is 11:44 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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