Documente Academic
Documente Profesional
Documente Cultură
(G651120714)
consum_1.cpp
PROCESS
Main thread runs
Creates addem thread
Main thread calls addem(5)
Main thread output:
The
The
The
The
The
The
The
value of i
value of i
value of i
value of i
value of i
value of i
sum is 15
is
is
is
is
is
is
0
1
2
3
4
5
value of i
value of i
value of i
value of i
value of i
value of i
sum is 15
is
is
is
is
is
is
0
1
2
3
4
5
Raditya W Erlangga
(G651120714)
consum_1.cpp in Unix (using Pthread)
#include <iostream>
using namespace std;
void* addem(void* count)
{
int sum = 0;
int ncount = (int) count;
//
Sleep(3000);
sleep(3000);
for (int i=0; i <= ncount; ++i) {
cout << "The value of i is " << i <<
endl;
sum += i;
}
cout << "The sum is " << sum << endl;
pthread_exit(0);
return 0;
}
int main()
{
pthread_t thread;
pthread_create(&thread,0, addem, (void*)5);
//
pthread_join(thread,0);
addem((void*)5);
return 0;
Raditya W Erlangga
(G651120714)
consum_2.cpp
PROCESS
Main thread runs
Creates ThreadFunction thread
Main thread calls MainFunction (5)
Main thread output:
Main:
Main:
Main:
Main:
Main:
Main:
Main:
The
The
The
The
The
The
The
value of i
value of i
value of i
value of i
value of i
value of i
sum is 15
is
is
is
is
is
is
0
1
2
3
4
5
The
The
The
The
The
The
The
The
The
The
The
The
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
sum is 55
is
is
is
is
is
is
is
is
is
is
is
0
1
2
3
4
5
6
7
8
9
10
TIME
Raditya W Erlangga
(G651120714)
consum_2.cpp in Unix (using Pthread):
#include <iostream>
#include <pthread.h>
using namespace std;
int addem(int count, const string& name)
{
int
sum = 0;
for (int i = 0; i<=count; ++i) {
cout << name << ": The value of i is " << i << endl;
sum += i;
}
cout << name << ": The sum is " << sum << endl;
return 0;
}
void* ThreadFunction(void* count)
{
int ncount = (int)count;
addem(ncount, "Thread");
pthread_exit(0);
return 0;
}
int MainFunction(int count)
{
addem(count, "Main");
return 0;
}
int main(int argc, char *argv[])
{
pthread_t thread;
pthread_create(&thread, 0, ThreadFunction, (void*)10);
MainFunction(5);
pthread_join(thread,0);
cout << "Program finished" << endl;
return 0;
}
Raditya W Erlangga
(G651120714)
consum_3.cpp
PROCESS
Main thread runs
Main thread allocates memory for threadinfo and
assigns 10 to threadinfo attribute value as the loop
counter
Main creates addem thread and passes threadinfo
object to addem thread
Main assigns 5 to threadinfo attribute value as the
loop counter and calls addem()
Main(master) thread output:
master:
master:
master:
master:
master:
master:
master:
the
the
the
the
the
the
the
value of i
value of i
value of i
value of i
value of i
value of i
sum is 15
is
is
is
is
is
is
0
1
2
3
4
5
TIME
the
the
the
the
the
the
the
the
the
the
the
the
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
value of i
sum is 55
is
is
is
is
is
is
is
is
is
is
is
0
1
2
3
4
5
6
7
8
9
10
Raditya W Erlangga
(G651120714)
consum_3.cpp in Unix (using Pthread):
#include <iostream>
#include <pthread.h>
using namespace std;
struct threadinfo {
public:
int value;
string id;
};
void* addem(void* pInfo)
{
int sum = 0, count = 0;
threadinfo* info = static_cast<threadinfo*>(pInfo);
count = info->value;
for (int i=0; i<=count; ++i) {
cout << info->id << ": the value of i is " <<
sum += i;
}
cout << info->id << ": the sum is " << sum << endl;
delete info;
info = 0;
pthread_exit(0);
return 0;
}
int main()
{
threadinfo* pInfo = 0;
pthread_t thread;
pInfo = new threadinfo;
pInfo->value = 10;
pInfo->id = "slave";
pthread_create(&thread, 0, addem, (void*) pInfo);
pInfo = new threadinfo;
pInfo->value = 5;
pInfo->id = "master";
addem(pInfo);
pthread_join(thread, 0);
return 0;
}
i << endl;
Raditya W Erlangga
(G651120714)
mutex_example.cpp
PROCESS
Main thread runs
Main thread creates 5 threads and mutex to
synchronize access of a global file descriptor
Main thread waits the 5 threads to finish their tasks
Each the 5 threads are using the shared file descriptor
synchronously.
Worker threads:
I am thread
Thread 0 is
Thread 0 is
I am thread
Thread 2 is
Thread 2 is
I am thread
Thread 4 is
Thread 4 is
I am thread
Thread 1 is
Thread 1 is
I am thread
Thread 3 is
Thread 3 is
0.
the very best.
in control.
2.
the very best.
in control.
4.
the very best.
in control.
1.
the very best.
in control.
3.
the very best.
in control.
TIME
Raditya W Erlangga
(G651120714)
mutex_example.cpp in Unix (using PThread):
#include
#include
#include
#include
<time.h>
<iostream>
<fstream>
<pthread.h>
*/
ofstream shared_fd("test.txt");
/* shared file descriptor
*/
pthread_mutex_t
hFileMutex;
/* file I/O mutual exclusion */
bool
Use_Mutex;
/* flag for using mutex
*/
/*
Thread function
*/
void* ThreadFunction(void* num)
{
cout << "call... ThreadFunc .. 1" << endl;
int nnum = (int) num;
/*
Take ownership of FileMutex, if used. If already
owned by another thread, the call will block for an
INFINITE amount of time until it is released.
*/
cout << "use_mutex " << Use_Mutex << endl;
if(Use_Mutex)
pthread_mutex_lock(&hFileMutex);
/*
Print to the shared file and flush output. Sleep between
prints to demonstrate contention if mutex is not used.
*/
cout << "call... ThreadFunc .. 2" << endl;
shared_fd << "I am thread " << nnum << endl;
sleep( 1 );
shared_fd << "Thread " << nnum << " is the very best." <<
endl;
//
sleep( 1 );
shared_fd << "Thread " << nnum << " is in control." <<
/*
Release ownership of FileMutex, if used.
*/
if(Use_Mutex)
pthread_mutex_unlock(&hFileMutex);
cout << "call... ThreadFunc .. 3" << endl;
endl;
Raditya W Erlangga
(G651120714)
return 0;
}
int main(int argc, char* argv[])
{
pthread_t hThread[NUM_THREADS];
/*
Mutual exclusion is ignored if there are any command
line arguments.
*/
if(argc > 1) Use_Mutex = false;
else Use_Mutex = true;
if(Use_Mutex)
pthread_mutex_init(&hFileMutex, 0);
/*
Use a pseudorandom number stream to introduce delays into
file access between the streams. srand() "seeds" the random
variable stream.
*/
srand((unsigned)time(NULL));
/*
Start NUM_THREADS threads.
*/
for(int i = 0; i < NUM_THREADS; ++i)
{
pthread_create(&hThread[i],0, ThreadFunction, (void*)i);
}
/*
Wait for all threads to finish.
*/
for(int j = 0; j < NUM_THREADS; j++)
{
cout << "call... inside loop .. " << j <<
pthread_join(hThread[j],0);
}
shared_fd.close();
return 0;
}
Output in test.txt:
I am thread
Thread 0 is
Thread 0 is
I am thread
Thread 1 is
Thread 1 is
I am thread
Thread 2 is
Thread 2 is
I am thread
Thread 3 is
Thread 3 is
I am thread
Thread 4 is
Thread 4 is
0
the very best.
in control.
1
the very best.
in control.
2
the very best.
in control.
3
the very best.
in control.
4
the very best.
in control.
endl;
Raditya W Erlangga
(G651120714)
server.cpp
client.cpp
WSAStartup()
Socket()
socket()
Socket Descriptor: 3
Socket Descriptor: 3
gethostbyname(localhost)
bind()
Address Length: 4
Return Code: 0
listen()
Return Code: 0
accept()
connect()
Return Code: 0
New Socket Descriptor: 5
Remote Port: 40904
Host Address: 127.0.0.1
gethostbyaddr(127.0.0.1)
Remote Host: localhost
recv()
recv()
Bytes received: 25
Message: Message from the server.
Raditya W Erlangga
(G651120714)
server.cpp
client.cpp
closesocket()
closesocket()
Return Code: 0
Return Code: 0