我有两个线程的多线程程序,我想将它转换为多进程程序,以便两个线程成为两个进程,它告诉哪个进程首先完成
这是mutlithreaded程序:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define OUTPUTNAME "write.out"
#define OUTPUTNAME1 "fprint.out"
void *syscall_writer_function();
void *stdlibrary_writer_function();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
/* Create independent threads each of which will execute functions */
if( (rc1=pthread_create( &thread1, NULL, &syscall_writer_function, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &stdlibrary_writer_function, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(EXIT_SUCCESS);
}
void *syscall_writer_function()
{
long i;
int fd;
if ((fd=open(OUTPUTNAME,O_WRONLY|O_CREAT,0644)) < 0){
fprintf(stderr,"Can't open %s. Bye.\n",OUTPUTNAME);
exit(1);
}
for (i=0; i<50000; i++) { /* write 50,000 Ys with write */
if (write(fd,"Y",1) < 1) {
fprintf(stderr,"Can't write. Bye\n");
exit(1);
}
}
pthread_mutex_lock( &mutex2 );
counter++;
printf("Syscall finished\n");
printf("Counter value: %d\n",counter);
close(fd);
pthread_mutex_unlock( &mutex2 );
}
void *stdlibrary_writer_function()
{
long i;
FILE *fp;
if ((fp=fopen(OUTPUTNAME1,"w")) == NULL) {
fprintf(stderr,"Can't open %s. Bye.\n",OUTPUTNAME1);
exit(1);
}
for (i=0; i<400000; i++) { /* write 400,000 Xs with fprintf */
if (fprintf(fp,"X") < 1) {
fprintf(stderr,"Can't write. Bye\n");
exit(1);
}
}
counter++;
printf("Stdlibrary finished\n");
printf("Counter value: %d\n",counter);
fclose(fp);
}
如何将其转换为C?
中的多进程程序