리눅스 프로그래밍 쓰레드 관련 문제 못풀겠어요~ 어떻게하죠?
글쓴이: ksm2day / 작성시간: 일, 2016/04/10 - 10:55오후
#include
#include
int hello(void);
int func1(void);
int main(int argc, char* argv[])
{
pthread_t id;
pthread_create(&id,NULL,func1,NULL);
printf("I'm main thread\n");
hello();
sleep(3);
return 0;
}
int hello(void) {
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("pid %u tid %u\n",(unsigned int)pid,(unsigned int)tid);
return 0;
}
int func1(void)
{
printf("I'm child thread\n");
hello();
return 0;
}
이 프로그램을 참고해서 두개의자식프로세스를 생성한 후에 메인 쓰레드는 'M',첫번째 자식쓰레드는'A',두번째자식쓰레드는'B'를 각각 10000번 출력하는것인데 어떻게 해야되나요?? ㅠㅠ 도와줘요!
Forums:
확실하지는 않습니다..
저같은 초보가 답변을 달아서 도움이 되겠냐만은,
그래도 생각나는 것이 있어서 답변을 드립니다.
간단한 echo 서버 예제입니다.
이 부분은 서버 코드고, 다수 클라이언트를 fork를 이용해서
처리하는 것 같습니다. while문 안에만 보시면 될 것 같네요..
제 생각엔 pid별로 구분하는 것 같습니다. 도움이 되셨으면 합니다.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
#define PORTNUM 3600
int main(int argc, char **argv)
{
int listen_fd, client_fd;
pid_t pid;
socklen_t addrlen;
int readn;
char buf[MAXLINE];
struct sockaddr_in client_addr, server_addr;
if((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return 1;
memset((void *)&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORTNUM);
if(bind(listen_fd, (struct sockaddr *)&server_addr,
sizeof(server_addr)) == -1)
{
perror("bind error");
return 1;
}
if(listen(listen_fd, 5) == -1)
{
perror("listen error");
return 1;
}
signal(SIGCHLD,SIG_IGN);
while(1)
{
addrlen = sizeof(client_addr);
client_fd = accept(listen_fd,
(struct sockaddr *)&client_addr, &addrlen);
if(client_fd == -1)
{
printf("accept error\n");
break;
}
pid = fork();
if(pid == 0)
{
memset(buf, 0x00, MAXLINE);
while((readn = read(client_fd, buf, MAXLINE)) > 0)
{
printf("Read Data %s (%d) : %s",
inet_ntoa(client_addr.sin_addr),
client_addr.sin_port,
buf);
write(client_fd, buf, strlen(buf);
memset(buf, 0x00, MAXLINE);
}
close(client_fd);
return 0;
}
else if(pid > 0)
close(client_fd);
}
return 0;
}
숙제는 구글에게..
숙제는 구글에게..
https://kldp.org/filter/tips
https://kldp.org/filter/tips 를 읽어보시고 소스를 올려주셔요.
세벌 https://sebuls.blogspot.kr/
저번 질문에서 바뀐게 하나도 없네요 -.-;;;
저번 질문에서 바뀐게 하나도 없네요 -.-;;;
https://kldp.org/node/155084
https://kldp.org/node/155084
저번 질문인데, 토씨하나 달라지지 않고 copy & paste 했네요.
스스로도 노력을 안하는데 남이 해줄까요?
업로드 날짜가 같은 것을 보니, 그냥 글이 중복으로
업로드 날짜가 같은 것을 보니, 그냥 글이 중복으로 올라간 것 같아요...
저는 이렇게 생각했습니다.
댓글 달기