Linux Tasks – RTOS Like(With Fork)
#ifndef APP_H_
#define APP_H_
#define NUM_OF_TASKS 7
typedef void (*task)();
typedef struct {
int p_ids[NUM_OF_TASKS];
}shared_data;
void tcpServerTask();
void tcpClientTask();
void uartTask1();
void uartTask2();
void writeDataTask();
void computeTask();
void spiTask();
typedef enum { TCPServer , TCPClient, UART1, UART2, WriteData, Compute, SPI } task_type;
#endif /* APP_H_ */
* main.c
*
* Created on: May 14, 2010
* Author: root
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "app.h"
task all_tasks[NUM_OF_TASKS] = {
tcpServerTask,
tcpClientTask,
uartTask1,
uartTask2,
writeDataTask,
computeTask,
spiTask
};
int main(int argc,char *argv[])
{
int i,pid,status,md,size;
shared_data *data;
size = sizeof(shared_data);
md = shm_open("/app_memory", O_CREAT|O_RDWR, 0);
if((ftruncate(md, size)) == -1){ /* Set the size */
perror("ftruncate failure");
exit(1);
}
data = mmap(0, size, PROT_WRITE, MAP_SHARED, md, 0);
for( i=0 ; i<NUM_OF_TASKS ; i++ )
{
if( (pid=fork()) == 0)
{
all_tasks[i]();
exit(0);
}
else
{
data->p_ids[i] = pid;
}
}
while(1)
{
pid=wait(&status);
switch (findpid(pid))
{
case TCPServer:
puts("tcp server terminated");
break;
case TCPClient:
puts("tcp client terminated");
break;
case UART1:
puts("uart1 terminated");
break;
case UART2:
puts("uart2 terminated");
break;
case WriteData:
puts("writedata terminated");
break;
case Compute:
puts("compute terminated");
break;
case SPI:
puts("spi terminated");
break;
default:
puts("error");
break;
}
}
return 0;
}
tcpClientTask.c
*
* Created on: May 15, 2010
* Author: root
*/
/*
inetclt.c Chapter 14
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/prctl.h>
#define MAXBUF 256
void tcpClientTask()
{
char msg[MAXBUF];
struct sockaddr_in addr = {0};
int n, sockfd;
puts("tcpClientTask started");
prctl(PR_SET_NAME,"TcpClient");
/* Create socket and connect to server */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(2000);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
sprintf (msg, "Test message from client %d", getpid());
while(1)
{
n = write(sockfd, msg, strlen(msg)); /* Send message */
n = read(sockfd, msg, MAXBUF); /* Read reply */
msg[n] = '\0';
printf ("client: %s\n", msg);
}
}
* tcpServerTask.c
*
* Created on: May 15, 2010
* Author: root
*/
/*
inetsrv.c Chapter 14
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <wait.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/prctl.h>
#define MAXBUF 256
#define SERVERPORT 2000
#define QUEUE_SIZE 5
void tcpServerTask() {
/* Note error handling not shown! */
char buffer[MAXBUF];
struct sockaddr_in addr;
struct sockaddr_in client;
unsigned int addrlen, n;
int sockfd, commfd;
puts("tcpServerTask started");
prctl(PR_SET_NAME,"TcpServer");
/* create and bind socket, set it up as a server */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVERPORT);
addr.sin_addr.s_addr = INADDR_ANY;
bind(sockfd, (struct sockaddr*) &addr, sizeof(addr));
listen(sockfd, QUEUE_SIZE);
/* Accept connection and deal with request */
memset(&client, 0, sizeof(client));
addrlen = sizeof(client);
commfd = accept(sockfd, (struct sockaddr*) &client, &addrlen);
if (commfd < 0) {
perror("accept");
exit(1); /* A real problem */
}
while (1) {
sleep(10);
n = read(commfd, buffer, MAXBUF);
buffer[n] = '\0';
printf("Message; %s\n", buffer);
write(commfd, "server: bye", 12);
}
close(commfd);
}
Recent Stories
Top DiscoverSDK Experts
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
X
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}Now comparing:
{{product.ProductName | createSubstring:25}} X
{{CommentsModel.TotalCount}} Comments
Your Comment