Linux (Ubuntu、12.04、3.8.13) で C++ のパイプを介して通信する 2 つのスレッドを取得しようとしています。あるスレッドからパイプに文字を書き込み、他のスレッドにそれを読み取らせて表示させたいだけです。
clone() 関数を使用してスレッドを作成しています。これは宿題に関連しているため、pthreads などを使用することはできません。
プログラム:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <fstream>
#include <cmath>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sched.h>
#include <fcntl.h>
using namespace std;
int p[2];//pipe
void read_pipe()
{
int c;
char charac[2];
read(p[0],charac,sizeof(charac));
//usleep(1000);
cerr<<"Read from pipe: "<<charac<<endl;
}
void write_pipe ()
{
write(p[1],"a",sizeof("a"));
cerr<<"Wrote to pipe: "<<"a"<<endl;
}
int thread1(void*)
{
close(p[0]);
write_pipe();
_exit(0);
}
int thread2(void*)
{
close (p[1]);
read_pipe();
_exit(0);
}
int main()
{
pipe(p);
char *stack_thread1=(char*)malloc(16384);
stack_thread1 +=16383;//stack grows downward -- gives seg fault otherwise
char *stack_thread2=(char*)malloc(16384);
stack_thread2 +=16383;//stack grows downward -- gives seg fault otherwise
int64_t elapsed_ns=0;
//create thread1
int tpid1=clone(thread1, (void*)stack_thread1, CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_VM, NULL);
cerr<<"\nThread1 created\n";
//create thread2
int tpid2=clone(thread2, (void*)stack_thread2, CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_VM, NULL);
cerr<<"\nThread2 created\n";
waitpid(tpid1,NULL,__WCLONE);//wait for clones to finish
waitpid(tpid2,NULL,__WCLONE);
usleep(5000);//make sure clones finished
cout<<"\nMain thread after sleep\n";
return 0;
}
出力は奇妙で、毎回異なります。たとえば、次のようになります。
Thread1 created
Thread2 created
Read from pipe:
Main thread after sleep
時々、エラーが表示されます: Illegal instruction. gdbで実行すると、違法な指示も出ます。
何が問題なのですか?
どんな助けでも大歓迎です!