Showing posts with label Embedded OS. Show all posts
Showing posts with label Embedded OS. Show all posts

Sunday, 25 March 2012

POSIX thread programming


Introduction:
Using thread has many benefits. They share the same resources of their calling process (parent process), like global variables, open files and so on. Also they have their own thread ID, stacks, and so on. For more information, please refer to the old article "Multitasking in Embedded Linux" and reference [1]. Here we focus on how to use it.

The simplest thread programming:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message(void *ptr)
{
    char *text;
    text=(char *)ptr;
    printf("%s \n", text);
}

main()
{
    pthread_t thread1, thread2;
    char *str1="I am thread1";
    char *str2="I am thread2";
    int T1ret, T2ret;

    /*Create two threads*/
    T1ret=pthread_create(&thread1, NULL, print_message, (void*) str1);
    T2ret=pthread_create(&thread2, NULL, print_message, (void*) str2);

    /*Wait for both threads to finish*/
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    printf("T1 & T2 return: %d, %d\n", T1ret, T2ret);
}
Here we use pthread_create function to create a thread. There are four arguments in it.
First argument: used to return the thread ID.
Second argument: used to set the attributes of thread, like priority, schedule policy and so on. For detail, please refer to reference [1].
Third argument: assign the thread function which is the name of the function.
Four argument: used to pass value.

Usually at the end of the calling function, we use pthread_join(threadid, NULL) to wait for termination of that thread. This function is very important, because sometimes we need to allocate some memories for threads (refer to reference [2]). After all the threads terminates, we need to free these memories.


How to pass value to thread:

We can only use the fourth argument of pthread_create() to pass value. And it is declared as void *, so it is a totally free local variable. It can be converted to any type of variables by using casting.

Passing of pointer:
This is easy, because the fourth argument is a void pointer itself. So you can use the pointer as fourth argument. In thread, you need to cast it to the  type you like.

Passing of value:
Two ways to do this.
1. In thread_create() function, cast the variable to be passed to void * type. Then in thread, you can cast it to any type you want. For example:
rc=pthread_create(&threads[t], NULL, printhello. (void *)t);
In thread:
void *printhello(void *threadid)
{
    long taskid=(long)threadid;
    ...
}
2. Allocate a or several memories using malloc( ) function (refer to [2]) which return a pointer points to that location. Then you can use it like using pointer we talked in part 1. Here must remember to free the location using free( ) at the end of the function who called malloc( ).
long *taskids[NUM_THREADS];

for(t=0; t<NUM_THREADS; t++)
{
   taskids[t] = (long *) malloc(sizeof(long));
   *taskids[t] = t;
   printf("Creating thread %ld\n", t);
   rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
   ...
}

Passing of multiple parameters:
Use structure (struct) to this. Imagine yourself.
struct thread_data{
   int  thread_id;
   int  sum;
   char *message;
};

struct thread_data thread_data_array[NUM_THREADS];

void *PrintHello(void *threadarg)
{
   struct thread_data *my_data;
   ...
   my_data = (struct thread_data *) threadarg;
   taskid = my_data->thread_id;
   sum = my_data->sum;
   hello_msg = my_data->message;
   ...
}

int main (int argc, char *argv[])
{
   ...
   thread_data_array[t].thread_id = t;
   thread_data_array[t].sum = sum;
   thread_data_array[t].message = messages[t];
   rc = pthread_create(&threads[t], NULL, PrintHello, 
        (void *) &thread_data_array[t]);
   ...
}

Synchronization using Mutex:

Initialization of mutex:
pthread_mutex_t mutex1=PTHREAD_MUTEX_INITIALIZER;

Lock a mutex:
pthread_mutex_lock(&mutex1);

Unlock a mutex:
pthread_mutex_unlock(&mutex1);

Destroy a mutex:
pthread_mutex_destroy(&mutex1);

Set the stack size of thread:

pthread_attr_t attr;    // Here we define a attribute variable.

size_t mystacksize;
pthread_attr_getstacksize(&attr, &mystacksize);    // Here we get the stack size of variable attr. Remember it
                                                                             // belongs to variable attr, not the thread who calling it.

pthread_attr_setstacksize(&attr, mystacksize);     // Here we set the stack size of variable attr. Also remember
                                                                           // it belongs to variable attr.

Remember to put variable attr into the second argument of function pthread_create( ), like:
pthread_create(&threads[t], &attr, dowork, (void *) t);

At last, remember to destroy attr:
pthread_attr_destroy(&attr);

This procedure also can be applied to other attributes setting like joinable, scheduling, priority and so on.

References:
[2] malloc - Linux man page, http://linux.die.net/man/3/malloc
[6] pthread_attr_destroy( ) -- Destroy a thread attributes object, http://cursuri.cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-g5.htm

Multitasking in Embedded Linux


Multitasking in Embedded Linux

In the computer field, "task" has the sense of a real-time application, as distinguished from process, which takes up space (memory), and execution time.

 A process, or task, can be seen as a set of allocated resources

Process switching makes big memory consumption. Because, when we created a new process, we need to create a process descriptor and allocate another memory region as its work space.

However, Thread allows you use the same memory in a process to work on, so you don't need to create an extra memory. So real-time linux use just one main process which includes threads to realize multitasking in embedded linux.

But there's more. Threads also happen to be extremely nimble. Compared to a standard fork(), they carry a lot less overhead. The kernel does not need to make a new independent copy of the process memory space, file descriptors, etc. That saves a lot of CPU time, making thread creation ten to a hundred times faster than new process creation. Because of this, you can use a whole bunch of threads and not worry too much about the CPU and memory overhead incurred. You don't have a big CPU hit the way you do with fork(). This means you can generally create threads whenever it makes sense in your program.
Threads differ from traditional multitasking operating system processes in that:
  • processes are typically mean time, while threads exist as subsets of a process
  • processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources.
  • processes have separate address spaces, whereas threads share their address space.
  • processes interact only through system-provided inter-process communication mechanisms.
  • Context switching between threads in the same process is typically faster than context switching between processes.

Saturday, 24 March 2012

How to create process in Linux


 In Linux, we use fork( ) to create a new process which is actually a copy of the calling process. The new process is a copy (the code after fork( ) function) of its parent  process. So if we do nothing about control flow, the child process will actually do the same thing the parent does.
Here we use the return value of fork( ) to distinguish the child and parent. In fact, the function fork( ) returns two values after it finishes. For the calling process (parent process), it returns the PID of child process in order that the calling process can manage it. However in child process, fork() will return 0. So by this, we can just use a "if(chpid)" to distinguish the twos and make them execute the different sequences.
Another very important thing is the fork only copy the code after the for( ) function. It does not care about the code above is except the definition part of variables.
#include <stdio.h>
#include <sched.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
    pid_t chpid;
    int fd, status, variable=9;
    char ch;
    fd=open("test.txt", O_RDONLY);
    chpid=fork();
    if(chpid!=0)
        wait(&status);
    else
    { /*Executed only by the child*/
        variable=42;
        close(fd);
        printf("The child has changed the variable to: %d\n", variable);
        printf("The child has also closed the file.\n");
        return(0);
    }
    printf("The variable is now: %d\n", variable);
    if(read(fd, &ch, 1)<0)
    {
        perror("READ failed");
        return(1);
    }
    printf("Read from the file: %s\n", &ch);
    return (0);
}
Above is common use case of fork( ). The wait( ) function is used by calling processes to wait for the state changes of their child processes, like resume, termination, restart and so on. For detail, please refer to reference [1].
References:
[1] wait - Linux man page, http://linux.die.net/man/2/wait

Thursday, 22 March 2012

Anonymous pipe and Named pipe


Introduction:
Anonymous pipe and Named pipe are usually used for communication between two different tasks.

Anonymous pipe:
It does not have name, and can be applied in the shell by using ' | '. It is unidirectional.
For example, p1 | p2
Here anonymous pipe usually is used in case of standard output and standard input which are related to the shell. So here you can use anonymous pipe ' | ' directly to transfer data from p1 to p2. So here must remember that the output and input are both for shell which means usually using functions printf() for output and scanf() for input or something like this.

Named pipe:
However, named pipe is related to a dev file which is more flexible. So in this case, you can transfer the data like writing/reading to/from file which is already familiar to you. We can create a pipe by using mkfifo /dev/mypipe where mkfifo is a new command, and in some old version of Linux you may use mknod p /dev/mypipe. But we recommend you use mkfifo /dev/mypipe.

Then we can write our applications like the following to test mypipe:
Read file:
//fifo_read.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
FILE *fp;
char buf[20];
/* Create the FIFO if it does not exist*/
umask(0);
mknod("mypipe", S_IFIO|0666, 0);
while(1)
{
fp=fopen("mypipe", "r");
fgets(buf, 20, fp);
printf("We received: %s\n", buf);
fclose(fp);
}
return(0);
}

Write file:
//fifo_write.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
FILE *fp;
if((fp=fopen("pipe", "w"))==NULL)
{
fprintf(stderr, "fopen caused an error\n");
exit(1);
}
fputs(argv[1], fp);
fclose(fp);
return(0);
}

Blocking and nonblocking:
Here we use fopen("mypipe", "r") open mypipe as a normal file which is default for blocking. So the after  writing, the program is blocked and wait for another program to read data. Similarly, after execute the read program, it will be blocked and wait until there is an another program to write in this buffer.

If it is nonblocking, then neither program will be waiting, they will just be executed until the end. But here we cannot use stream functions anymore, because they do not have the option for nonblocking, so you have to use file descriptors which is operated using system call (like operating drivers). So in file descriptor we can open the buffer as nonblocking as following:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int ifd;
...
ifd=open("/dev/mypipe", O_RDONLY | O_NONBLOCK);
...
read(ifd, buf, 20);

The differences between streams and file descriptors:


Warning:
Unless you really really have to use nonblocking buffer, please use stream to do blocking buffer.

Wednesday, 22 February 2012

Microkernel(relative to monolithic kernel)


Microkernel(relative to monolithic kernel)
Microkernel is the near-minimum amount of software that can provide the mechanisms needed to implement an operating system (OS). From this sentence, we can get several points below:
1) Microkernel is very small.
2) Microkernel contains some program such as low-level address space management, thread management, scheduling, IPC(inter-process communication) and so on which are all very basic tools.
3) The functionalities mentioned above are all very necessary for a OS kernel(here kernel don't need to provide these features any more, maybe just some drivers, internet module or something else). So Microkernel OS is much more portable because of its small size.
4) Here other operating systems are implemented on user mode above the microkernel.
5) From the picture below, we can see that an obvious difference between Microkernel and Monolithic kernel is that application in Monolithic kernel communicate with kernel level through system call. However application in Microkernel OS communicate with drivers, servers, and other applications through IPC instead of system call.

As a microkernel must allow building arbitrary operating system services on top, it must provide some core functionality. At the least, this includes:
    1) some mechanisms for dealing with address spaces — this is required for managing memory protection;
    2) some execution abstraction to manage CPU allocation — typically threads or scheduler activations; and
    3) inter-process communication — required to invoke servers running in their own address spaces.


Wednesday, 15 February 2012

TQ2440 linux 移植


这里使用的都是天嵌公司提供的资源来移植的。

内核编译:

首先编译系统内核,这个可以按照《linux移植之step by step》的附录5来一步步操作。当然我已经将配置好的config文件保存起来了,即/opt/EmbedSky/linux-2.6.30.4/arch/arm/configs/s3c2410_defconfig。(这里一定要注意一点,配置完内核后要保存配置信息,同时还要保存为另一个配置文件名称.config,否则系统会按默认的配置进行内核编译。),执行make和make zImage来编译内核。
编译内核时,要使用root用户,否则会出现各种莫名其妙的错误。

根文件系统编译:

接着创建根文件系统,首先创建一系列目录以及目录中相关的文件,然后使用命令mkyaffs2image命令来将整个文件夹制作成根文件系统镜像。
以后,每次编译完新的程序就可以放到该文件系统目录中,然后再编译整个目录,再下载到板子中。

如果每次都将根文件系统下载到flash中,不但损伤flash,同时也会浪费时间(写入flash的时间要比写入SDRAM的时间要长)。所以最好可以把文件系统直接下载到SDRAM来进行程序调试。基本上有两种方法可以提供:
  • 使用u-boot中的tftp指令来将内核或者根文件系统下载到SDRAM。
  • 使用openjtag,用软件openocd来直接将内核和文件系统下载到指定位置。
用TFTP协议来下载文件(ubuntu10.10):

1, 首先下载tftp软件包 - tftp(客户端), tftpd(服务器), xinetd(网络请求的守护进程, 当xinetd接到连接时,它能够确定连接所需的程序,启动相应的进程,并把socket交给它)。
sudo apt-get install xinetd tftpd tftp

2, 创建tftp配置文件/etc/xinetd.d/tftp。
sudo vi /etc/xinetd.d/tftp
配置如下:
service tftp
{
           protocol = udp
           port = 69
           socket_type = dgram
           wait = yes
           user = nobody
           server = /usr/sbin/in.tftpd
           server_args = /tftpboot
           disable = no
}

3, 产生服务器目录/tftpboot:
sudo mkdir /tftpboot
sudo chmod -R 777 /tftpboot
sudo chown -R nobody /tftpboot

4, 重新启动xinetd服务:
sudo /etc/init.d/xinetd stop
sudo /etc/init.d/xinetd start

5, 测试tftp server是否设置好:
tftp 192.168.0.123 (服务器ip地址)
tftp>  get zImage
tftp>  quit

6, 更改有线网卡的ip地址。
sudo ifconfig eth0 192.168.0.123  (这里ip地址对应于板子上uboot的设置,注意eth0一定是你连接的那个网卡)

7, 你也可以查看和设置板子上uboot的参数
printenv   (查看uboot的参数,包括serverip和ipaddr等)
setenv serverip 192.168.0.123  (更改uboot的参数,同理可以更改ipaddr等)
saveenv     (更改完参数后一定要记住保存,否则重启后参数更新)

6, 完毕,现在就可以利用网络来下载内核及文件系统
uboot>  tftp 30000000 zImage  (S3C2440的SDRAM区间从30000000开始)
uboot>  tftp 30008000 root.bin

使用tftp的注意事项:

纠结了一天,始终连接不上的原因是选错网卡了,现在的笔记本一般都既有兼有有线网卡和无线网卡,由于我们要使用有线网卡,所以一定要注意选对网卡,然后再更改ip地址。

下一步要研究一下如何可以在板子上的linux系统通过网络mount主机(host)的目录,这样有助于调试的方便。



引用:



Tuesday, 14 February 2012

How to compile drivers into linux kernel


The working principle of kernel compiling:

There are files need to change in the relevant directory - Kconfig and Makefile.
  • All these configures and makefiles are based on the great program "make".
  • When we enter "make menuconfig", the "make" program will build a visual configure interface according to Kconfig files existing in different directories.
  • After finish configuring the system, "make" will produce a configure file ".config" which contains the information of your configure options.
  • Then compile the system using "make", gcc will compile the system according to the Makefiles which will choose to compile drivers as kernel, or module, or not according to the configure file ".config".
The format of Kconfig file:

menu "Name of menu"

config SYMBOL1                                      
           tristate (or bool) "The function of this option"
           default M (or Y)
           ---help---
           Here we can illustrate what is the configure option used for

config SYMBOL2                                      
           tristate (or bool) "The function of this option"
           default M (or Y)
           ---help---
           Here we can illustrate what is the configure option used for

config SYMBOL3                                      
           tristate (or bool) "The function of this option"
           default M (or Y)
           ---help---
           Here we can illustrate what is the configure option used for

...

endmenu

"config SYMBOL" here config is a keyword which illustrates the following symbol is a configure option, and symbol is used to identity the configure which will be changed into CONFIG_SYMBOL and be valued as Y, M, or empty in .config file. Then further, Makefile can use these CONFIG_SYMBOLs to choose to compile drivers as kernel, module or not compile.

There is another useful keyword of Kconfig - source.
This keyword is used in the parent directory which usually is used to enter the Kconfig file of subdirectory.
Its usage is shown below:

menu "Name of the menu"

source "currentdirectoty/subdirectory1/Kconfig"
source "currentdirectoty/subdirectory2/Kconfig"
source "currentdirectoty/subdirectory3/Kconfig"
source "currentdirectoty/subdirectory4/Kconfig"
source "currentdirectoty/subdirectory5/Kconfig"
...

endmenu

The root of Kconfig structure:

Kconfig files make a configure tree of kernel. So there must be a root which can be used as the entry of kernel configure system. You must modify the root Kconfig file to add driver or modify driver, otherwise it cannot display when you do "make menuconfig". So the root Kconfig file is : /arch/xxx(processor architecture)/Kconfig. This is the entry (main menu) of  the whole configure interface of kernel.

How to write Makefile according to Kconfig:

After writing Kconfig file, we need to write Makefile to response to Kconfig.
We already knew that the Makefile need to use the configure file .config to compile the source code.
So we need to use the SYMBOLs we talked above to choose to compile drivers as kernel (Y), or module (M) or not compile (empty).
It is written as following:

obj - $(CONFIG_SYMBOL1) (tab tab ...) += xxxx1.o  
obj - $(CONFIG_SYMBOL2) (tab tab ...) += xxxx2.o  
obj - $(CONFIG_SYMBOL3) (tab tab ...) += xxxx3.o  

After change the Makefile in the current directory, you have to modify the Makefile in the parent directory to add the path of your new drivers, otherwise when compiling the kernel, it cannot find your new driver. This sentence should be like following:

obj - $(CONFIG_SYMBOL1) (tab tab ...) += mydrivers/ 
obj - $(CONFIG_SYMBOL2) (tab tab ...) += mydrivers/
obj - $(CONFIG_SYMBOL3) (tab tab ...) += mydrivers/  



References:
[1] Linux 内核源码树学习: Kconfig 和 Makefile http://www.linuxidc.com/Linux/2009-11/23091.htm
[2] Linux source code.
[3] Linux设备驱动开发详解, P61.


Sunday, 22 January 2012

Startup in linux


Startup in linux:
1, In linux, file /etc/init.d/rc.local is the startup file of linux. So if you want to start some commend when the system boot, you can write the commend or script at the end of the file.
2, If you want to mount some disk, first you need to be sure the disk wasn't mounted on some place else. Otherwise, you need to unmount the dev file before the mount the file on the target.

Saturday, 19 November 2011

Common commands in Linux

VI operations:
dd is used to delete one line
yy is used to copy one line
p is used to paste one line

GCC some useful operations:
-g is used to compile the program so that the debug program GDB can debug the execution.
-l is used to link the library, for example, -lm means to link "math" library.
-o used to name the execution file.

GDB some useful commands:
list : used to list the program in the source code.
break (line number) : used to set the break point.
run : run the program in GDB.
next(or n) : used to execute program step by step.
continue : execute the program until the end.
quit(or q) : exit GDB.
(press ENTER) : reexecute the last command.

Chapter 3 — The Molt

(This story is written by me and I will be posting it on Royal Road. Profile: https://www.royalroad.com/profile/1129550/fictions) Six fifty ...