writing PIDs using pthreads

I need to write a program that creates 2 threads and writes their PIDs into a file each other time. as in pid a, pid b, pid a, pid b etc. into the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

int main()
{
        pid_t pid;



pid=fork();
fork();
if(pid==0){
        printf("pid: %d", getpid());
}

return(0);
}


got this so far, that writes out my 2 PIDs. but I also want to loop it, tho looping a fork isn't really good. I've been adviced to use pthreads, but I've got no idea how that works, and I've been having a hard time googling pthreads to find any decent explanation of how it works. either way, I'd want to run thru this session atleast 5 times so I can get some data into a file.
Topic archived. No new replies allowed.