리눅스 프로그래밍 쓰레드 관련 문제 못풀겠어요~ 어떻게하죠?

ksm2day의 이미지

#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번 출력하는것인데 어떻게 해야되나요?? ㅠㅠ 도와줘요!

익명 사용자의 이미지

저같은 초보가 답변을 달아서 도움이 되겠냐만은,
그래도 생각나는 것이 있어서 답변을 드립니다.
간단한 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 를 읽어보시고 소스를 올려주셔요.

chocokeki의 이미지

저번 질문에서 바뀐게 하나도 없네요 -.-;;;

twinwings의 이미지

https://kldp.org/node/155084

저번 질문인데, 토씨하나 달라지지 않고 copy & paste 했네요.

스스로도 노력을 안하는데 남이 해줄까요?

HDNua의 이미지

업로드 날짜가 같은 것을 보니, 그냥 글이 중복으로 올라간 것 같아요...

저는 이렇게 생각했습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.