콘텐츠로 바로가기

now0930 일지

이런저런 생각

  • 홈
  • 비공개
  • 강좌
  • 잔여 작업 조회
  • 위치

thread 예제

linux system programming, 234p

https://bitsoul.tistory.com/m/157 여기 참조.

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>


void* start_routine(void* arg ){
	int pid;
	int threadID;
	pid= getpid();
	printf("pid is %d\n", pid);
	threadID = pthread_self();
	printf("tid is %lu\n",threadID);
	printf("data is %s\n",arg);
	}

int main(){
	pthread_t thread[2];
	const char *message1="hello one";
	const char *message2="hello two";
	const char *message3="hello main";

	int ret, errorno;
	ret = pthread_create(&thread[0],NULL,start_routine,(void*)message1);
	if(ret<0){
		errorno = ret;
		//printf("%d\n",errorno);
		perror("pthread_create");
		return -1;
	}
	ret = pthread_create(&thread[1],NULL,start_routine,(void*)message2);
	if(ret<0){
		errorno = ret;
		//printf("%d\n",errorno);
		perror("pthread_create");
		return -1;
	}



	/*
	for(int i=0;i<2;i++){
		ret = pthread_create(&thread[i],NULL,start_routine,NULL);
		if(!ret){
			errorno = ret;
			printf("%d\n",errorno);
			perror("pthread_create");
			return -1;
		}
	}
	*/
	int thread_compare=0;
	//스레드가 다르면 0, 같으면 0이 아는 수 리턴.
	//다른 스레드로 0을 리턴.
	thread_compare = pthread_equal(thread[0], thread[1]);
	printf("thread is same? %d\n",thread_compare);
	sleep(1);
	//메인에서  start_routine.
	start_routine((void*)message3);


	//각 스레드 끝나길 대기

	pthread_join(thread[0],NULL);
	pthread_join(thread[1],NULL);
	return 0;

}

위 코드를 실행하면 아래와 같다. race condition으로 매 실행 다른 결과를 보았다.

pi@raspberrypi:~/Project/cCode/systemProgram $ gcc -lpthread mythread.c ;./a.out 
thread is same? -1235590048
pid is 14996
tid is 3059377248
data is hello two
pid is 14996
tid is 3067769952
data is hello one
pid is 14996
tid is 3069673600
data is hello main
pi@raspberrypi:~/Project/cCode/systemProgram $ gcc -lpthread mythread.c ;./a.out 
pid is 15068
tid is 3067769952
data is hello one
pid is 15068
tid is 3059377248
data is hello two
thread is same? 0
pid is 15068
tid is 3069673600
data is hello main
pi@raspberrypi:~/Project/cCode/systemProgram $ gcc -lpthread mythread.c ;./a.out 
pid is 15125
tid is 3068077152
data is hello one
thread is same? 0
pid is 15125
tid is 3059684448
data is hello two
pid is 15125
tid is 3069980800
data is hello main

gnu c는 pthread가 전부인가 보다.

이 글 공유하기:

  • Tweet
발행일 2020-06-28글쓴이 이대원
카테고리 생활코딩 태그 c, linux, system program

댓글 남기기응답 취소

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.

글 내비게이션

이전 글

daemon 예제

다음 글

mutex 예제

2025 5월
일 월 화 수 목 금 토
 123
45678910
11121314151617
18192021222324
25262728293031
4월    

최신 글

  • common mode, differential mode 2025-05-11
  • signal conditioner, 신호 처리기 2025-05-10
  • strain gage 2025-05-09
  • 칼만 필터 2025-05-01
  • positioner(I/P) 2025-04-26

카테고리

  • 산업계측제어기술사
  • 삶 자국
    • 책과 영화
    • 투자
  • 생활코딩
    • LEGO
    • ROS
    • tensorflow
  • 전기기사
  • 피아노 악보

메타

  • 로그인
  • 엔트리 피드
  • 댓글 피드
  • WordPress.org

페이지

  • 소개
  • 잔여 작업 조회
    • 작업 추가
    • 작업의 사진 조회
    • 작업 수정 페이지
  • 사진
    • GPS 입력된 사진
    • 사진 조회
  • 위치
    • 하기 휴가 방문지
    • 해외 출장

태그

android bash c docker driver FSM gps java kernel LEGO linux mysql network program opcua open62541 plc programmers python raspberry reinforcementLearning ros state space system program tensorflow transfer function 경제 미국 민수 삼국지 세계사 실기 에너지 역사 유전자 일본 임베디드 리눅스 전기기사 조선 중국 채윤 코딩 테스트 통계 한국사 한국어

팔로우하세요

  • Facebook
now0930 일지
WordPress로 제작.