Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:46:40+00:00 2026-06-12T08:46:40+00:00

hi there i have a work about a programme which is like one child

  • 0

hi there i have a work about a programme which is like one child should print a number into a text file and the second child should take that number to print it onto screen simultaneously. but my code is work like first child finishes to printing the numbers 0 through 9 and then second child starts to read them onto screen. so i guess its a synchronization issue. here is my simple code ;

#include <stdio.h>     /* basic I/O routines.   */
#include <unistd.h>    /* define fork(), etc.   */
#include <sys/types.h> /* define pid_t, etc.    */
#include <sys/wait.h>  /* define wait(), etc.   */
#include <signal.h>    /* define signal(), etc. */
#include <pthread.h>

void write_num(void);
void print_screen(void);
//void catch_child(int);

int main(int argc, char* argv[]) {

        int i, result, pid;

        pid = fork(); /* creates a new process, you only want the parent to spawn children? */

        switch (pid) {

             case -1:
                /* fork error */
                printf("Error occured with fork()\n");
                exit(1);
             case 0:
                /* child process */
                write_num(); 
                exit(0);
             default:
                 /* parent process*/
                {
                //wait(&pid);
                pid = fork(); /* fork new children here */


                switch(pid) {

                    case -1:
                        printf("Error occured with fork()\n");
                        exit(1);

                    case 0: 

                        print_screen();
                        exit(0);

                    default:
                        break;

                         }
                }
     }
    wait(&pid);
    execl("/usr/bin/killall","killall","tail",(char *) 0);
    return 0;
}

void write_num(void){

 FILE* fptr;
 int i;

 fptr=fopen("textfile.txt","w");

    for(i=0; i<10; i++){

        fprintf(fptr,"%d\n",i);
        fflush(stdout);
        sleep(1);

        }
}

void print_screen(void){

        execl("/usr/bin/tail","tail","-f","./textfile.txt",(char *) 0);
        sleep(1);

}

/* first, here is the code for the signal handler
void catch_child(int sig_num)
{
     when we get here, we know there's a zombie child waiting
    int child_status;

    wait(&child_status);
    printf("child exited.\n");
}*/

by the way, in Ubuntu i used to compile with gcc -o process process.c -lpthread.

I will be appreciated if you can help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:46:41+00:00Added an answer on June 12, 2026 at 8:46 am

    You need to change to

    flush(fptr);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to work on a database in which there are many stored-procedures &
Is there a way to have the file reference work under ipad? The file
Problem I have a file which contains such text (nothing else) 2010-10-05 I would
We have one perplexity about current situation with TFS. There are 3 branches: Develop,
Suppose I have a scenario like below : There are about 225 Computers having
Is there any way to do it? I often have issues that work locally
I have a python program that must work on Windows and Linux. There are
At work, we have a report that is generated with IP's in it. There
I frequently work with the System.IO namespace. Is there a way to have that
I have three textareas using tinymce as text area. They all work in chrome

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.