Tuesday 3 April 2012

Pthread Example


Set up Ubuntu to use static ip address

 had bit problem when using tftp server to boot NGW100, every time DHCP tend to find a different ip address for my PC, which is not what I wanted;I don’t want to change NGW100 boot args everytime to use different TFTP server or NFS server.
Here is what I do:
If Your Ubuntu System has set to use DHCP, you will want to change it to a static IP address here is simple tip
open the /etc/network/interfaces file.
sudo vi /etc/network/interfaces
If you are using DHCP for your primary network card which is usually eth0, you will see the following lines
auto eth0
iface eth0 inet dhcp
As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should add and here is the example and you can change these settings according to your network settings.
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
Restart the neworking service using the following command
sudo /etc/init.d/networking restart
My interfaces file like this:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
        address 192.168.1.59
        netmask 255.255.255.0
        broadcast 192.168.1.255
        network 192.168.1.0
        gateway 192.168.1.2

PThread example
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    void *print_message_function( void *ptr );
    main()
    {
         pthread_t thread1, thread2;
         char *message1 = “Thread 1″;
         char *message2 = “Thread 2″;
         int  iret1, iret2;
        /* Create independent threads each of which will execute function */
         iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
         iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
         /* Wait till threads are complete before main continues. Unless we  */
         /* wait we run the risk of executing an exit which will terminate   */
         /* the process and all threads before the threads have completed.   */
         pthread_join( thread1, NULL);
         pthread_join( thread2, NULL);
         printf(“Thread 1 returns: %d “,iret1);
         printf(“Thread 2 returns: %d “,iret2);
         exit(0);
    }
    void *print_message_function( void *ptr )
    {
         char *message;
         message = (char *) ptr;
         printf(“%s “, message);
    }

No comments:

Post a Comment