-1

In study course i need to use 2 programs in same time to writing strings in one file.

I typed two similar programs on C:

#include <stdio.h>
int main(int argc, char** argv)
{
   FILE *f;
   f = fopen("output.txt", "w+");
   while (1)
   {
      fprintf(f, "%s", "kill me pls \n");
   }
   return 0;
}

and

#include <stdio.h>
int main(int argc, char** argv)
{
   FILE *f;
   f = fopen("output.txt", "w+");
   while (1)
   {
      fprintf(f, "%s", " NO! \n");
   }
   return 0;
}

And then i compiled and tried to run this programs in same time, using command

./prog1 & ./prog2 &

But nothing happening. In console i saw:

stolz$ ./prog1 & ./prog2 &
[7] 3920
[8] 3921

How must i type shell command to run this programs in the same time?

Claus Stolz
  • 358
  • 4
  • 18
  • 1
    What is your expectation after running such a test? – babon Oct 05 '17 at 05:39
  • 2
    First of all neither program really outputs anything. Then to make matters worse your two programs attempt to write to the same file, at the same time, leading to a race condition. – Some programmer dude Oct 05 '17 at 05:40
  • As for your question, the output shown (the lines starting with the opening square brackets) is the shell telling you that it started two background processes. Meaning your programs will be running at the same time as independent processes. – Some programmer dude Oct 05 '17 at 05:41
  • @babon i expect that they will work together with one file – Claus Stolz Oct 05 '17 at 05:47
  • @NicholasGoncharov The program is running simultaneously, but as it is stated earlier it will lead to a race condition. So, the output written to the file is not synchronized and you cannot expect one print after another. – Gaurav Pathak Oct 05 '17 at 05:49
  • Yes, they do work "together" with one file and it is hard to predict which process will write to the file before another. – babon Oct 05 '17 at 05:51
  • Consider the file position. If you use `w+` mode then the positioning is at the beginning of the file. The second program (whichever that is) will overwrite what the first program wrote. Even if you use `a+` then buffering could still produce a conflict. – cdarke Oct 05 '17 at 07:14

1 Answers1

2

How must i type shell command to run this programs in the same time?

The way you proposed in your question was the right way:

$ ./prog1 & ./prog2 &
[7] 3920
[8] 3921

That starts both of the programs running in the background at the same time.

What happens then: Your code opens the output.txt using fopen w+. man fopen tells us:

w+  Open for reading and writing.  The file is created if it does not
    exist, otherwise it is truncated.  The stream is positioned at the
    beginning of the file.

Lets change that to a+ and modify your code by adding a sleepand a fflush for it:

#include <stdio.h>
#include <unistd.h>                        # that also need this
int main(int argc, char** argv)
{
   FILE *f;
   f = fopen("output.txt", "a+");          # changed w+ to a+
   while (1)
   {
      fprintf(f, "%s", "kill me pls \n");
      fflush(f);                           # this for that:
      sleep(1);                            # that
   }
   return 0;
}

Make the above changes to prog2.c also, compile them:

$ gcc -o prog1 prog1.c ; gcc -o prog2 prog2.c

and start them and tail the output.txt:

$ ./prog1 & ./prog2 &
[1] 6976
[2] 6977
$ tail -f output.txt 
kill me pls 
kill me pls 
kill me pls 
 NO! 
kill me pls 
 NO! 
kill me pls 
 NO! 
kill me pls 
James Brown
  • 36,089
  • 7
  • 43
  • 59