Explore

Loading

Wednesday, March 7, 2012

Creating thread in linux.


THREADS, LIKE PROCESSES, ARE A MECHANISM TO ALLOW A PROGRAM to do more than
one thing at a time.As with processes, threads appear to run concurrently; the Linux
kernel schedules them asynchronously, interrupting each thread from time to time to
give others a chance to execute.
Conceptually, a thread exists within a process.Threads are a finer-grained unit of
execution than processes.When you invoke a program, Linux creates a new process
and in that process creates a single thread, which runs the program sequentially.That
thread can create additional threads; all these threads run the same program in the
same process, but each thread may be executing a different part of the program at any
given time.
We’ve seen how a program can fork a child process.The child process is initially
running its parent’s program, with its parent’s virtual memory, file descriptors, and so
on copied.The child process can modify its memory, close file descriptors, and the like
without affecting its parent, and vice versa.When a program creates another thread,
though, nothing is copied.The creating and the created thread share the same memory
space, file descriptors, and other system resources as the original. If one thread changes
the value of a variable, for instance, the other thread subsequently will see the modified
value. Similarly, if one thread closes a file descriptor, other threads may not read
from or write to that file descriptor. Because a process and all its threads can be executing
only one program at a time, if any thread inside a process calls one of the exec
functions, all the other threads are ended (the new program may, of course, create new
threads).
GNU/Linux implements the POSIX standard thread API (known as pthreads). All
thread functions and data types are declared in the header file <pthread.h>.The
pthread functions are not included in the standard C library. Instead, they are in
libpthread, so you should add -lpthread to the command line when you link your
program.
4.1 Thread Creation
Each thread in a process is identified by a thread ID.When referring to thread IDs in
C or C++ programs, use the type pthread_t.
Upon creation, each thread executes a thread function.This is just an ordinary function
and contains the code that the thread should run.When the function returns, the
thread exits. On GNU/Linux, thread functions take a single parameter, of type void*,
and have a void* return type.The parameter is the thread argument: GNU/Linux passes
the value along to the thread without looking at it.Your program can use this parameter
to pass data to a new thread. Similarly, your program can use the return value to
pass data from an exiting thread back to its creator.
The pthread_create function creates a new thread.You provide it with the following:
1. A pointer to a pthread_t variable, in which the thread ID of the new thread is
stored.
2. A pointer to a thread attribute object.This object controls details of how the
thread interacts with the rest of the program. If you pass NULL as the thread
attribute, a thread will be created with the default thread attributes.Thread
attributes are discussed in Section 4.1.5,“Thread Attributes.”
3. A pointer to the thread function.This is an ordinary function pointer, of this
type:
void* (*) (void*)
4. A thread argument value of type void*. Whatever you pass is simply passed as
the argument to the thread function when the thread begins executing.
A call to pthread_create returns immediately, and the original thread continues executing
the instructions following the call. Meanwhile, the new thread begins executing
the thread function. Linux schedules both threads asynchronously, and your program
must not rely on the relative order in which instructions are executed in the two
threads.

The program in Listing 4.1 creates a thread that prints x’s continuously to standard
error. After calling pthread_create, the main thread prints o’s continuously to standard
error.
Listing 4.1 (thread-create.c) Create a Thread
#include <pthread.h>
#include <stdio.h>
/* Prints x’s to stderr. The parameter is unused. Does not return. */
void* print_xs (void* unused)
{
while (1)
fputc (‘x’, stderr);
return NULL;
}
/* The main program. */
int main ()
{
pthread_t thread_id;
/* Create a new thread. The new thread will run the print_xs
function. */
pthread_create (&thread_id, NULL, &print_xs, NULL);
/* Print o’s continuously to stderr. */
while (1)
fputc (‘o’, stderr);
return 0;
}
Compile and link this program using the following code:
% cc -o thread-create thread-create.c -lpthread
Try running it to see what happens. Notice the unpredictable pattern of x’s and o’s as
Linux alternately schedules the two threads.
Under normal circumstances, a thread exits in one of two ways. One way, as illustrated
previously, is by returning from the thread function.The return value from the
thread function is taken to be the return value of the thread. Alternately, a thread can
exit explicitly by calling pthread_exit.This function may be called from within the
thread function or from some other function called directly or indirectly by the thread
function.The argument to pthread_exit is the thread’s return value.


New Open Tablet Will Come with Linux and KDE Preinstalled

Sure enough, that's just what came out over the weekend.
Specifically, KDE developer Aaron Seigo on Saturday revealed that a 7-inch Linux tablet named “Spark” will soon be available with KDE Plasma Active as its default user interface. Brought to market by the KDE Plasma Active community itself under the brand “Make-Play-Live,” it will be priced at 200 euros, or roughly $260.

 Along with Plasma Active, the Spark device will offer a 1GHz AMLogic ARM processor, Mali-400 GPU, 512 MB of RAM, 4GB of internal storage, an SD card slot, a 7-inch capacitive multitouch screen, and Wi-Fi connectivity.
credits:http://www.pcworld.com/businesscenter/article/248947/new_open_tablet_will_come_with_linux_and_kde_preinstalled.html

Which should I use, BSD or Linux?

What does this all mean in practice? Who should use BSD, who should use Linux?
This is a very difficult question to answer. Here are some guidelines:
  • “If it ain't broke, don't fix it”: If you already use an open source operating system, and you are happy with it, there is probably no good reason to change.
  • BSD systems, in particular FreeBSD, can have notably higher performance than Linux. But this is not across the board. In many cases, there is little or no difference in performance. In some cases, Linux may perform better than FreeBSD.
  • In general, BSD systems have a better reputation for reliability, mainly as a result of the more mature code base.
  • BSD projects have a better reputation for the quality and completeness of their documentation. The various documentation projects aim to provide actively updated documentation, in many languages, and covering all aspects of the system.
  • The BSD license may be more attractive than the GPL.
  • BSD can execute most Linux binaries, while Linux can not execute BSD binaries. Many BSD implementations can also execute binaries from other UNIX like systems. As a result, BSD may present an easier migration route from other systems than Linux would.

What is BSD?

BSD stands for “Berkeley Software Distribution”. It is the name of distributions of source code from the University of California, Berkeley, which were originally extensions to AT&T's Research UNIX operating system. Several open source operating system projects are based on a release of this source code known as 4.4BSD-Lite. In addition, they comprise a number of packages from other Open Source projects, including notably the GNU project. The overall operating system comprises:


  • The BSD kernel, which handles process scheduling, memory management, symmetric multi-processing (SMP), device drivers, etc.
    Unlike the Linux kernel, there are several different BSD kernels with differing capabilities.
  • The C library, the base API for the system.
    The BSD C library is based on code from Berkeley, not the GNU project.
  • Utilities such as shells, file utilities, compilers and linkers.
    Some of the utilities are derived from the GNU project, others are not.
  • The X Window system, which handles graphical display.
    The X Window system used in most versions of BSD is maintained by the X.Org project. FreeBSD allows the user to choose from a variety of desktop environments, such as Gnome, KDE, or Xfce; and lightweight window managers like Openbox, Fluxbox, or Awesome.
  • Many other programs and utilities.


    CREDITS:http://www.freebsd.org/doc/en/articles/explaining-bsd/