Re: Unterschied Thread - Prozess
I. Prozeß:
1. Eigener Speicherbereich auf dem HEAP (wird angelegt falls
"new"-Operator (OOP) oder malloc verwendet wird).
2. Eigener Stack (wird belegt für Parameterzwischenspeicherung
bei Unterprogrammaufrufen).
(Rest für den Unterscheid nicht primär von Bedeutung.)
II. Thread (oder auch leichtgewichtiger Prozeß):
1. Kein EIGENER Speicherbereich auf dem Heap. Der Speicherbereich wird unter allen Threads die unter dem
selben Vaterthread (Thread-ID = 0) laufen aufgeteilt, d.h.
jeder dieser Threads ist zugriffsberechtigt.
-> Problem:
"Race Conditions" durch Zugriff auf atomare Bereiche.
-> Lösung: Stichworte -> Semaphore, condition variable,
sleep, wake-up, signals, condition wait ...
2. Eigener Stack für Unterprogrammaufrufe wie "schwergewichtige
Prozesse".
-----------------------------------------------------------
Beispielprogramm dazu:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream.h>
void* print_message_function(void *);
int main(int argc, char** argv)
/* [<][>][^][v][top][bottom][index][help] */
{
pthread_t thread1, thread2;
pthread_attr_t attr;
char* message1 = "This is Thread 1";
char* message2 = "This is Thread 2";
#ifdef __Lynx__
pthread_attr_create(&attr);
#else
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
#endif
#ifdef __Lynx__
cout<<"__LYNX__"<<endl;
pthread_create(&thread1, attr, print_message_function, (void*) message1);
pthread_create(&thread2, attr, print_message_function, (void*) message2);
#else
pthread_create(&thread1, &attr, print_message_function, (void*) message1);
pthread_create(&thread2, &attr, print_message_function, (void*) message2);
#endif
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
#ifdef __Lynx__
cout<<"__LYNX__"<<endl;
pthread_attr_delete(&attr);
#else
pthread_attr_destroy(&attr);
#endif
printf("\n\nProgramm wird nun beendet\n\n");
return 0;
}
void* print_message_function(void *ptr)
/* [<][>][^][v][top][bottom][index][help] */
{
char* message;
message = (char *) ptr;
for(int i=0;i<10;++i)
printf("%s \n", message);
return(0);
}
------------------------------------------------------
III. Mischform aus Thread und Prozeß
Es ist möglich für einen schwergewichtigen Prozeß
"shared-momory" zu alloziieren (shmget(...)-Befehl).
-----------------------------------------------------------
Beispielprogramm dazu:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <time.h>
#include <iostream.h>
#define NUMBER_ORCS 20
/* shared value */
void main()
{
int nOrcKill = 1;
int pid;
// variables related to share memory
char *myAddr;
key_t key;
int shmflg;
int shmid;
int size;
int shmcmd;
struct shmid_ds buf;
printf("Paladin : I come to serve\n");
// initialize variable for shared memory
key = IPC_PRIVATE;
size = 1000;
shmflg = 0660; // & IPC_CREAT & IPC_EXCL;
// create share memory
if ( (shmid = shmget(key, size, shmflg)) == -1) {
printf("Paladin : Fail to create share memory!\n");
exit(1);
} else
printf("Paladin : Share memory created with id %d.\n",shmid);
// attach a pointer to share memory
if ( (myAddr = static_cast<char*>(shmat(shmid,NULL,SHM_RND))) == (char *)-1) {
printf("Parent \t: Fail in attaching to share memory!\n");
exit(1);
}
myAddr[0] = 0;
if ((pid = fork()) == 0) {
/* I am child */
sleep(1);
printf("Footman : I come to serve!\n");
// initialize random seed
srandom(time(NULL));
// attach to share memory
if ( (myAddr = static_cast<char*>(shmat(shmid,NULL,SHM_RND))) == (char *)-1) {
printf("Footman : Fail in attaching to share memory!\n");
exit(1);
}
while (myAddr[0] < NUMBER_ORCS) {
if (random() > RAND_MAX/2)
sleep(1);
printf("Footman : I've killed %d Orcs!\n",nOrcKill++);
myAddr[0]++;
};
// delete share memory linkage
shmdt(myAddr);
} else {
/* I am parent */
sleep(1);
printf("Paladin : Yes! My Lord!\n");
// initialize random seed
srandom(time(NULL));
while (myAddr[0] < NUMBER_ORCS) {
if (random() > RAND_MAX/2)
sleep(1);
printf("Paladin : I've killed %d Orcs!\n",nOrcKill++);
myAddr[0]++;
};
// delete share memory linkage
shmdt(myAddr);
if (shmctl(shmid, IPC_RMID, &buf) == -1) {
printf("Share Memory Removal Failure!\n");
exit(1);
}
}
}
-------------------------------------------------------------
Gruß, M.
[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]