![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 2
| problem implementing fork 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);
}
}
}
Code: dheeraj@dheeraj-machine:~/linux_pro$ ./a.out 1000 temp Code: rc: 1024000 ########################### done. |
| dheerajsuthar is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,259
| Code: while(1){
printf("#");
fflush( stdout );
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Sep 2009
Posts: 2
| |
| dheerajsuthar is offline | |
![]() |
| Tags |
| fork, linux, system call |
| Thread Tools | |
| Display Modes | |
|
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 |