[c언어] 스레드와 프로세스의 관계
C언어에서 스레드와 프로세스는 다중 실행을 가능케하는 두 가지 주요 개념입니다. 이들은 서로 연관되어 있지만 다른 개념이기도 합니다.
스레드(Thread)란?
스레드는 프로세스 내에서 실제 작업이 이루어지는 실행 단위입니다. 각 프로세스는 하나 이상의 스레드를 가질 수 있으며, 이러한 스레드들은 같은 주소 공간을 공유합니다. 이는 데이터와 코드를 공유하여 메모리를 절약하고 작업을 효율적으로 처리할 수 있게 합니다.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
int main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
pthread_create(&thread1, NULL, print_message_function, (void*) message1);
pthread_create(&thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
프로세스(Process)란?
프로세스는 실행 중인 프로그램을 의미하며, 각 프로세스는 독립적인 메모리 공간을 가지고 있습니다. 각 프로세스는 운영체제로부터 자원(메모리, CPU 시간 등)을 할당받아 실행됩니다.
#include <stdio.h>
int main()
{
int pid;
pid = fork();
if (pid == 0) {
printf("This is the child process\n");
}
else {
printf("This is the parent process\n");
}
return 0;
}
따라서, C언어에서 스레드와 프로세스는 서로 다른 실행 단위를 나타내며, 프로세스는 독립된 실행 환경을 제공하는 반면, 스레드는 프로세스의 자원을 공유하는 실행 단위를 나타냅니다.
이 둘은 병행성 및 병렬성에 관련된 여러 측면에서 중요한 역할을 합니다.
참고 자료
- GeeksforGeeks, “Threads vs Processes”: https://www.geeksforgeeks.org/multithreading-c-2/
- Tutorialspoint, “C - Multithreading”: https://www.tutorialspoint.com/multithreading-in-c
- TutorialsTeacher, “C - Process Management”: https://www.tutorialsteacher.com/csharp/csharp-process
- TutorialsPoint, “C - Process”: https://www.tutorialspoint.com/operating_system/os_processes.htm