Why is my C program output not in the same order as when I redirect it to a file ?
by Sxing from LinuxQuestions.org on (#5RXV2)
It's a multi-threading program synchronized with semaphore. According to my knowledge, those three thread should print one by one, at least in this way.
And if I redirect it to a file, it seems the lines are organized by thread and the print line in father thread is repeated 3 times ???
I am a rookie in multi-threading programming. please help me.
compiled with gcc -pthread on Ubuntu x86/64
Code:#include <sys/sem.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/wait.h>
int main()
{
int pipe_fd[2];
int status = 20;
char buffer[4096]; //0Y
pid_t pid1, pid2, pid3;
sem_t *pipe_mutex;
pipe_mutex = sem_open("pipe_mutex", O_CREAT | O_RDWR, 0666, 0);
printf("init\n");
if ((pid1 = fork()) == 0)
{
printf("child1 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child1 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 128);
fprintf(stdout, "child1 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else if ((pid2 = fork()) == 0)
{
printf("child2 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child2 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 2047);
fprintf(stdout, "child2 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else if ((pid3 = fork()) == 0)
{
printf("child3 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child3 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 4200);
fprintf(stdout, "child3 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else
{
// getchar(); //type anything and enter to start
sem_post(pipe_mutex);
wait(0);
wait(0);
wait(0);
// pipe_left_size(pipe_fd, pipe_mutex); //
printf("end\n");
// sem_unlink("pipe_mutex");
}
return 0;
}
in a file:
init
child1 init.
child1 start writing
child1 write 20
child1 start writing
child1 write 19
child1 start writing
child1 write 18
child1 start writing
child1 write 17
child1 start writing
child1 write 16
child1 start writing
child1 write 15
child1 start writing
child1 write 14
child1 start writing
child1 write 13
child1 start writing
child1 write 12
child1 start writing
child1 write 11
child1 start writing
child1 write 10
child1 start writing
child1 write 9
child1 start writing
child1 write 8
child1 start writing
child1 write 7
child1 start writing
child1 write 6
child1 start writing
child1 write 5
child1 start writing
child1 write 4
child1 start writing
child1 write 3
child1 start writing
child1 write 2
child1 start writing
child1 write 1
child1 start writing
child1 write 0
init
child2 init.
child2 start writing
child2 write 20
child2 start writing
child2 write 19
child2 start writing
child2 write 18
child2 start writing
child2 write 17
child2 start writing
child2 write 16
child2 start writing
child2 write 15
child2 start writing
child2 write 14
child2 start writing
child2 write 13
child2 start writing
child2 write 12
child2 start writing
child2 write 11
child2 start writing
child2 write 10
child2 start writing
child2 write 9
child2 start writing
child2 write 8
child2 start writing
child2 write 7
child2 start writing
child2 write 6
child2 start writing
child2 write 5
child2 start writing
child2 write 4
child2 start writing
child2 write 3
child2 start writing
child2 write 2
child2 start writing
child2 write 1
child2 start writing
child2 write 0
init
child3 init.
child3 start writing
child3 write 20
child3 start writing
child3 write 19
child3 start writing
child3 write 18
child3 start writing
child3 write 17
child3 start writing
child3 write 16
child3 start writing
child3 write 15
child3 start writing
child3 write 14
child3 start writing
child3 write 13
child3 start writing
child3 write 12
child3 start writing
child3 write 11
child3 start writing
child3 write 10
child3 start writing
child3 write 9
child3 start writing
child3 write 8
child3 start writing
child3 write 7
child3 start writing
child3 write 6
child3 start writing
child3 write 5
child3 start writing
child3 write 4
child3 start writing
child3 write 3
child3 start writing
child3 write 2
child3 start writing
child3 write 1
child3 start writing
child3 write 0
init
end
in terminal:
init
child2 init.
child2 start writing
child2 write 20
child2 start writing
child2 write 19
child2 start writing
child2 write 18
child2 start writing
child2 write 17
child2 start writing
child2 write 16
child2 start writing
child2 write 15
child2 start writing
child2 write 14
child2 start writing
child2 write 13
child2 start writing
child2 write 12
child2 start writing
child2 write 11
child2 start writing
child2 write 10
child2 start writing
child2 write 9
child2 start writing
child2 write 8
child2 start writing
child2 write 7
child2 start writing
child2 write 6
child2 start writing
child2 write 5
child2 start writing
child2 write 4
child2 start writing
child2 write 3
child2 start writing
child2 write 2
child2 start writing
child2 write 1
child2 start writing
child2 write 0
child1 init.
child1 start writing
child1 write 20
child1 start writing
child1 write 19
child1 start writing
child1 write 18
child1 start writing
child1 write 17
child1 start writing
child1 write 16
child1 start writing
child1 write 15
child1 start writing
child1 write 14
child1 start writing
child1 write 13
child3 init.
child1 start writing
child3 start writing
child1 write 12
child1 start writing
child1 write 11
child1 start writing
child1 write 10
child3 write 20
child1 start writing
child3 start writing
child1 write 9
child3 write 19
child1 start writing
child3 start writing
child1 write 8
child3 write 18
child1 start writing
child3 start writing
child1 write 7
child3 write 17
child1 start writing
child3 start writing
child3 write 16
child1 write 6
child3 start writing
child1 start writing
child3 write 15
child1 write 5
child3 start writing
child1 start writing
child3 write 14
child1 write 4
child3 start writing
child1 start writing
child3 write 13
child1 write 3
child3 start writing
child1 start writing
child3 write 12
child1 write 2
child3 start writing
child1 start writing
child3 write 11
child1 write 1
child3 start writing
child1 start writing
child3 write 10
child1 write 0
child3 start writing
child3 write 9
child3 start writing
child3 write 8
child3 start writing
child3 write 7
child3 start writing
child3 write 6
child3 start writing
child3 write 5
child3 start writing
child3 write 4
child3 start writing
child3 write 3
child3 start writing
child3 write 2
child3 start writing
child3 write 1
child3 start writing
child3 write 0
end
And if I redirect it to a file, it seems the lines are organized by thread and the print line in father thread is repeated 3 times ???
I am a rookie in multi-threading programming. please help me.
compiled with gcc -pthread on Ubuntu x86/64
Code:#include <sys/sem.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/wait.h>
int main()
{
int pipe_fd[2];
int status = 20;
char buffer[4096]; //0Y
pid_t pid1, pid2, pid3;
sem_t *pipe_mutex;
pipe_mutex = sem_open("pipe_mutex", O_CREAT | O_RDWR, 0666, 0);
printf("init\n");
if ((pid1 = fork()) == 0)
{
printf("child1 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child1 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 128);
fprintf(stdout, "child1 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else if ((pid2 = fork()) == 0)
{
printf("child2 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child2 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 2047);
fprintf(stdout, "child2 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else if ((pid3 = fork()) == 0)
{
printf("child3 init.\n");
// fflush(stdout);
while (status >= 0)
{
sem_wait(pipe_mutex);
fprintf(stdout, "child3 start writing\n");
// fflush(stdout);
// status = write(pipe_fd[1], buffer, 4200);
fprintf(stdout, "child3 write %d\n", status);
// fflush(stdout);
// sleep(1);
sem_post(pipe_mutex);
status--;
}
exit(0);
}
else
{
// getchar(); //type anything and enter to start
sem_post(pipe_mutex);
wait(0);
wait(0);
wait(0);
// pipe_left_size(pipe_fd, pipe_mutex); //
printf("end\n");
// sem_unlink("pipe_mutex");
}
return 0;
}
in a file:
init
child1 init.
child1 start writing
child1 write 20
child1 start writing
child1 write 19
child1 start writing
child1 write 18
child1 start writing
child1 write 17
child1 start writing
child1 write 16
child1 start writing
child1 write 15
child1 start writing
child1 write 14
child1 start writing
child1 write 13
child1 start writing
child1 write 12
child1 start writing
child1 write 11
child1 start writing
child1 write 10
child1 start writing
child1 write 9
child1 start writing
child1 write 8
child1 start writing
child1 write 7
child1 start writing
child1 write 6
child1 start writing
child1 write 5
child1 start writing
child1 write 4
child1 start writing
child1 write 3
child1 start writing
child1 write 2
child1 start writing
child1 write 1
child1 start writing
child1 write 0
init
child2 init.
child2 start writing
child2 write 20
child2 start writing
child2 write 19
child2 start writing
child2 write 18
child2 start writing
child2 write 17
child2 start writing
child2 write 16
child2 start writing
child2 write 15
child2 start writing
child2 write 14
child2 start writing
child2 write 13
child2 start writing
child2 write 12
child2 start writing
child2 write 11
child2 start writing
child2 write 10
child2 start writing
child2 write 9
child2 start writing
child2 write 8
child2 start writing
child2 write 7
child2 start writing
child2 write 6
child2 start writing
child2 write 5
child2 start writing
child2 write 4
child2 start writing
child2 write 3
child2 start writing
child2 write 2
child2 start writing
child2 write 1
child2 start writing
child2 write 0
init
child3 init.
child3 start writing
child3 write 20
child3 start writing
child3 write 19
child3 start writing
child3 write 18
child3 start writing
child3 write 17
child3 start writing
child3 write 16
child3 start writing
child3 write 15
child3 start writing
child3 write 14
child3 start writing
child3 write 13
child3 start writing
child3 write 12
child3 start writing
child3 write 11
child3 start writing
child3 write 10
child3 start writing
child3 write 9
child3 start writing
child3 write 8
child3 start writing
child3 write 7
child3 start writing
child3 write 6
child3 start writing
child3 write 5
child3 start writing
child3 write 4
child3 start writing
child3 write 3
child3 start writing
child3 write 2
child3 start writing
child3 write 1
child3 start writing
child3 write 0
init
end
in terminal:
init
child2 init.
child2 start writing
child2 write 20
child2 start writing
child2 write 19
child2 start writing
child2 write 18
child2 start writing
child2 write 17
child2 start writing
child2 write 16
child2 start writing
child2 write 15
child2 start writing
child2 write 14
child2 start writing
child2 write 13
child2 start writing
child2 write 12
child2 start writing
child2 write 11
child2 start writing
child2 write 10
child2 start writing
child2 write 9
child2 start writing
child2 write 8
child2 start writing
child2 write 7
child2 start writing
child2 write 6
child2 start writing
child2 write 5
child2 start writing
child2 write 4
child2 start writing
child2 write 3
child2 start writing
child2 write 2
child2 start writing
child2 write 1
child2 start writing
child2 write 0
child1 init.
child1 start writing
child1 write 20
child1 start writing
child1 write 19
child1 start writing
child1 write 18
child1 start writing
child1 write 17
child1 start writing
child1 write 16
child1 start writing
child1 write 15
child1 start writing
child1 write 14
child1 start writing
child1 write 13
child3 init.
child1 start writing
child3 start writing
child1 write 12
child1 start writing
child1 write 11
child1 start writing
child1 write 10
child3 write 20
child1 start writing
child3 start writing
child1 write 9
child3 write 19
child1 start writing
child3 start writing
child1 write 8
child3 write 18
child1 start writing
child3 start writing
child1 write 7
child3 write 17
child1 start writing
child3 start writing
child3 write 16
child1 write 6
child3 start writing
child1 start writing
child3 write 15
child1 write 5
child3 start writing
child1 start writing
child3 write 14
child1 write 4
child3 start writing
child1 start writing
child3 write 13
child1 write 3
child3 start writing
child1 start writing
child3 write 12
child1 write 2
child3 start writing
child1 start writing
child3 write 11
child1 write 1
child3 start writing
child1 start writing
child3 write 10
child1 write 0
child3 start writing
child3 write 9
child3 start writing
child3 write 8
child3 start writing
child3 write 7
child3 start writing
child3 write 6
child3 start writing
child3 write 5
child3 start writing
child3 write 4
child3 start writing
child3 write 3
child3 start writing
child3 write 2
child3 start writing
child3 write 1
child3 start writing
child3 write 0
end