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

  • SEARCH
  • Home
  • 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 8667691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:01:52+00:00 2026-06-12T18:01:52+00:00

hi there a have an assignment on multiprocess programming and i’m facing with a

  • 0

hi there a have an assignment on multiprocess programming and i’m facing with a problem when i try to read characters line by line (each line consists of 3 character and 1 integer). the problem is when i use fscanf it doesnt work properly and it reads the characters successfully just in every 2 steps. here you can see my code;

#include <stdio.h>     /* basic I/O routines.   */
#include <stdlib.h>
#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>
#include <ctype.h>

void child_process(int);
void parent_process();
void function();
int counter=0;
int bond_number=0;

char* f_strand;

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

    counter = atoi(argv[1]);

    function();

    fflush(stdout);

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

void function(){

    int i,k;
    pid_t child_pid[counter];
    char array[counter];
    char array_opst[counter];
    srand ( time(NULL) );
    int temp;

    FILE* fptr;
    fptr = fopen("sample.txt","w");

    if(! (f_strand=(char*)malloc(counter*sizeof(char))) ){
        printf("Error\n");
        exit(1);
    }

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

        temp = rand()%4;

        if(temp==0){
            *(f_strand+i) = 'A';
            fprintf(fptr,"A\n");
        }
        else if(temp==1){
            *(f_strand+i) = 'C';
            fprintf(fptr,"C\n");
        }
        else if(temp==2){
            *(f_strand+i) = 'T';
            fprintf(fptr,"T\n");
        }
        else if(temp==3){
            *(f_strand+i) = 'G';
            fprintf(fptr,"G\n");
        }
    }

    fclose(fptr);

                    for (i = 0; i < counter; i++) {
                      if ( (child_pid[i] = fork() ) < 0) {

                        perror("fork");
                        abort();
                      } 
                      else if (child_pid[i] == 0) {

                        child_process(i);
                        exit(0);
                      }
                    }
    int status;
    pid_t pid;
    int num=counter;
        while (num > 0) {
          pid = wait(&status);
          //printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
          --num;  // Remove pid from the pids array.
        }


    parent_process();
    printf("\n");

}

void child_process(int index){

    int i;
    char str,str1,str2;

    FILE* fptr;
    fptr = fopen("sample.txt","r");

    fseek (fptr, 2*index, SEEK_SET);
    fscanf(fptr,"%c", &str);

    FILE* fptr2;
    fptr2 = fopen("sample_2.txt","a");

       if( str == 'A' ){
            str1='T';
            str2=';';
            fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
            }
       else if( str == 'T' ){
            str1='A';
            str2=';';
            fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
            }
       else if( str == 'G' ){
            str1='C';
            str2=':';
            fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
            }
       else if( str == 'C' ){
            str1='G';
            str2=':';
            fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
            }

    fflush(stdout);
    fclose(fptr);
    fclose(fptr2);
    sleep(1);

}

void parent_process(void){

    FILE* fptr;
    fptr = fopen("sample_2.txt","r");

    char str,str1,str2;
    int index,i,k;
    char array_opst[counter]; //  second strand
    char array_type[counter]; // bond type

    i=0;

    while( !feof(fptr) ){

        if(4==fscanf(fptr,"%c%c%c%d",&str,&str1,&str2,&index)){

            if(feof(fptr))
                break;

            printf("STR = %c\n, K = %d",str,k);

            array_type[index]=str1;
            array_opst[index]=str2;
            printf("WHILE\n");

            if(array_type[index] == ':')
                bond_number=bond_number+2;
            else if(array_type[index] == ';')
                bond_number=bond_number+3;
    }

        i++;
}
    fclose(fptr);

}

this is the output of sample2.txt

C:G5
G:C6
A;T4
C:G3
G:C7
C:G8
G:C2
A;T9
T;A1
C:G0

in the parent_process() it tries to read 3 characters and one integer line by line from sample2.txt file. but it does this process in every two lines. i mean it reads first third fifth lines and goes on. i will be appreciated if you can help and thanks anyway.

  • 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-12T18:01:53+00:00Added an answer on June 12, 2026 at 6:01 pm

    This is an interesting multifaceted issue. The first thing to note is that you’re using the c conversion. The C99 standard states as follows (§7.19.6.2¶8).

    Input white-space characters (as specified by the isspace function) are skipped, unless
    the specification includes a [, c, or n specifier.

    Now, let’s take a look at the important line.

        if(4==fscanf(fptr,"%c%c%c%d",&str,&str1,&str2,&index)){
    

    What occurs is that whitespace (the padding between the entries, the line terminator, etc.) is being consumed by fscanf. When this occurs, the read is misaligned such that it fails to read an integer when encountering the d conversion. As a result, fscanf does not evaluate to 4, the branch is not taken and thus the incorrectly read data isn’t printed.

    Instead, it requires incorrectly reading every other line to ‘realign’ to the proper input and thus you only see these select lines printed. To evidence this, try replacing the condition with merely 0 < fscanf(...) and the incorrect reads will produce the output to demonstrate the intermediate attempts at reading every other line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there exceptions for types which can't have thier assignment operator overloaded? Specifically, I'm
I have this assignment to prove that this problem: Finite alphabet £, two strings
I have a class assignment to read in data using Scanner. import java.util.Scanner; public
I have an assignment to read a file and output the average test scores.
I'm currently completing an assignment that involves using multithreading and multiprocess programming for the
There I have this assignment where I am suppose to emulate the hexdump function
I have assignment using R and have a little problem. In the assignment several
If I have an assignment Long c = a + b; Is there an
There is a DISTINCT operator in SQL. However, I have an assignment in which
I have an assignment in college in which we are required to read in

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.