Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main thread pid is 14608
- child thread pid is 14610
- main thread pid is 3615
- child thread pid is 3615
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- void* thread_function (void* arg)
- {
- fprintf (stderr, "child thread pid is %dn", (int) getpid ());
- /* Spin forever. */
- while (1);
- return NULL;
- }
- int main ()
- {
- pthread_t thread;
- fprintf (stderr, "main thread pid is %dn", (int) getpid ());
- pthread_create (&thread, NULL, &thread_function, NULL);
- /* Spin forever. */
- while (1);
- return 0;
- }
- long tid = syscall(SYS_gettid);
- printf("%ldn", tid);
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <syscall.h>
- void* thread_function (void* arg)
- {
- long tid = syscall(SYS_gettid);
- printf("child thread TID is %ldn", tid);
- fprintf (stderr, "child thread pid is %dn", (int) getpid ());
- /* Spin forever. */
- while (1);
- return NULL;
- }
- int main ()
- {
- pthread_t thread;
- long tid = syscall(SYS_gettid);
- printf("main TID is %ldn", tid);
- fprintf (stderr, "main thread pid is %dn", (int) getpid ());
- pthread_create (&thread, NULL, &thread_function, NULL);
- /* Spin forever. */
- while (1);
- return 0;
- }
- main TID is 17963
- main thread pid is 17963
- thread TID is 17964
- child thread pid is 17963
- // simple program to create threads that simply sleep
- // compile in debian jessie with apt-get install build-essential
- // and then g++ -O4 -Wall -std=c++0x -pthread threads2.cpp -o threads2
- #include <string>
- #include <iostream>
- #include <thread>
- #include <chrono>
- // how many seconds will the threads sleep for?
- #define SLEEPTIME 100
- // how many threads should I start?
- #define NUM_THREADS 25
- using namespace std;
- // The function we want to execute on the new thread.
- void threadSleeper(int threadid){
- // output what number thread we've created
- cout << "task: " << threadid << "n";
- // take a nap and sleep for a while
- std::this_thread::sleep_for(std::chrono::seconds(SLEEPTIME));
- }
- void main(){
- // create an array of thread handles
- thread threadArr[NUM_THREADS];
- for(int i=0;i<NUM_THREADS;i++){
- // spawn the threads
- threadArr[i]=thread(threadSleeper, i);
- }
- for(int i=0;i<NUM_THREADS;i++){
- // wait for the threads to finish
- threadArr[i].join();
- }
- // program done
- cout << "Donen";
- return;
- }
- UID PID PPID LWP C NLWP STIME TTY TIME CMD
- debian 689 687 689 0 1 14:52 ? 00:00:00 sshd: debian@pts/0
- debian 690 689 690 0 1 14:52 pts/0 00:00:00 -bash
- debian 6217 690 6217 0 1 15:04 pts/0 00:00:00 screen
- debian 6218 6217 6218 0 1 15:04 ? 00:00:00 SCREEN
- debian 6219 6218 6219 0 1 15:04 pts/1 00:00:00 /bin/bash
- debian 6226 6218 6226 0 1 15:04 pts/2 00:00:00 /bin/bash
- debian 6232 6219 6232 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6233 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6234 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6235 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6236 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6237 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6238 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6239 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6240 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6241 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6242 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6243 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6244 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6245 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6246 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6247 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6248 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6249 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6250 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6251 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6252 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6253 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6254 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6255 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6256 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6232 6219 6257 0 26 15:04 pts/1 00:00:00 ./threads2
- debian 6260 6226 6260 0 1 15:04 pts/2 00:00:00 ps -eLf
Add Comment
Please, Sign In to add comment