Fork two processes that create and remove 10,000 random length strings of random alphabetic characters.
Hint: use shared memory, include sys/shm.h.
See also Producer and consumer processes
SOLUTION
Download file
Excercise from CS3008
Saturday, 17 December 2011
Friday, 16 December 2011
Producer and consumer processes
Fork two processes which share a common 4KB area of memory. One process should produce 10 million bytes, the other should consume them.
Hint: use shared memory, include sys/shm.h. Think to what is fork function doing.
SOLUTION
Download the file here
Exercise from CS3008
Hint: use shared memory, include sys/shm.h. Think to what is fork function doing.
SOLUTION
Download the file here
Exercise from CS3008
Thursday, 15 December 2011
Write a ppm file picture
Write a picture ppm file to memory.
To make it faster, use mmap so that the memory writes itself back to a file at the end.
To make it faster, use mmap so that the memory writes itself back to a file at the end.
Wednesday, 14 December 2011
Reading man pages
The manual page for getpwuid says, among other things
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
The passwd structure is defined as follows:
struct passwd {
char *pw_name; /* user name */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
1. Where is struct passwd defined?
2. What is uid_t?
3. Write a minimum C program to print your real name and your user name
SOLUTION
1) A definition for struct passwd is provided by the <pwd.h> header
2) uid_t is a type defined by <sys/types.h> used for numerical user ID
3)
Exercise from: CS3008
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
The passwd structure is defined as follows:
struct passwd {
char *pw_name; /* user name */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
1. Where is struct passwd defined?
2. What is uid_t?
3. Write a minimum C program to print your real name and your user name
SOLUTION
1) A definition for struct passwd is provided by the <pwd.h> header
2) uid_t is a type defined by <sys/types.h> used for numerical user ID
3)
Exercise from: CS3008
Tuesday, 13 December 2011
Create a file with C
Write void makefile(const char * filename, int length); which writes a file of the specified length. The contents of the file should all be 0.
Exercise from: CS3008
Exercise from: CS3008
Saturday, 10 December 2011
C char and string
An easy exercise to get in touch with C char and strings handling
Write
void show( const char s[])
which prints the ascii values of the first ten chars in s.
Write a function
int mylen( const char * s)
which returns the useful length of the string s. C strings are terminated by a zero char.
Please do not use strlen from the string.h library, but write it with raw C. [1]
Test this with several examples, including the null string "".
Use your function as part of a program to count the number of 4 letter words typed to it. The central part is
char buff[100];
while (scanf(" %s", buff) != EOF){
// do something with buff, which should hold
// a zero-terminated string.
}
a.out expects you to type words to it, followed by an end of file (CTRL-D).
Try
cat /usr/dict/words | a.out
SOLUTION
#include <stdio.h>
void show(const char s[]){
int i = 0;
while(i<10){
printf("%d\n",s[i]);
i++;
}
}
int mylen( const char * s){
int cont = 0;
while(s[cont]!='\0'){
cont++;
}
return cont;
}
int countFourLettersWords(){
char buff[100];
int cont = 0;
while(scanf("%s",buff)!= EOF){
if(mylen(buff)==4)
cont++;
}
return cont;
}
int main(){
char s[]="Hello world";
char empty[]="";
char num[]="01234";
show(s);
printf("Length:\n %d\n",mylen(s));
show(empty);
printf("Length:\n %d\n",mylen(empty));
show(num);
printf("Length:\n %d\n",mylen(num));
printf("I will count the four letters words:\n");
printf("%d \n",countFourLettersWords());
getchar();
}
Exercise from: CS3008
Write
void show( const char s[])
which prints the ascii values of the first ten chars in s.
Write a function
int mylen( const char * s)
which returns the useful length of the string s. C strings are terminated by a zero char.
Please do not use strlen from the string.h library, but write it with raw C. [1]
Test this with several examples, including the null string "".
Use your function as part of a program to count the number of 4 letter words typed to it. The central part is
char buff[100];
while (scanf(" %s", buff) != EOF){
// do something with buff, which should hold
// a zero-terminated string.
}
a.out expects you to type words to it, followed by an end of file (CTRL-D).
Try
cat /usr/dict/words | a.out
SOLUTION
#include <stdio.h>
void show(const char s[]){
int i = 0;
while(i<10){
printf("%d\n",s[i]);
i++;
}
}
int mylen( const char * s){
int cont = 0;
while(s[cont]!='\0'){
cont++;
}
return cont;
}
int countFourLettersWords(){
char buff[100];
int cont = 0;
while(scanf("%s",buff)!= EOF){
if(mylen(buff)==4)
cont++;
}
return cont;
}
int main(){
char s[]="Hello world";
char empty[]="";
char num[]="01234";
show(s);
printf("Length:\n %d\n",mylen(s));
show(empty);
printf("Length:\n %d\n",mylen(empty));
show(num);
printf("Length:\n %d\n",mylen(num));
printf("I will count the four letters words:\n");
printf("%d \n",countFourLettersWords());
getchar();
}
Exercise from: CS3008
Friday, 9 December 2011
Structs
Little exercise to test yor skills with C structs
- 1) From the C statement
A[j].data[k] = 63;
What do we know about the types of
j
k
data
A
- 2) Write a definition for A which will make the statement syntactically acceptable.
- 3) Write a function to fill your version of A with a different value in every cell.
Possible Solution
- 1)
j int k int data int * A pointer to a structure TCell (see below)
- 2)
typedef struct TCell *Array;
struct TCell{
int * data;
}
Array A;
- 3)
typedef struct TCell *Array;
struct TCell{
int * data;
}
int main(){
int arrayDim = 10;
int dataDim = 5;
Array A = (Array)malloc(sizeof(struct TCell)*10);
int * data = (int *)malloc(sizeof(int)*dataDim);
for(int j=0; j < arrayDim; j++){
A[j].data = (int *)malloc(sizeof(int)*dataDim);
for( int k = 0; k < dataDim; k++){
A[j].data[k] = rand()%100;
}
}
}
Exercise from: CS3008
- 1) From the C statement
A[j].data[k] = 63;
What do we know about the types of
j
k
data
A
- 2) Write a definition for A which will make the statement syntactically acceptable.
- 3) Write a function to fill your version of A with a different value in every cell.
Possible Solution
- 1)
j int k int data int * A pointer to a structure TCell (see below)
- 2)
typedef struct TCell *Array;
struct TCell{
int * data;
}
Array A;
- 3)
typedef struct TCell *Array;
struct TCell{
int * data;
}
int main(){
int arrayDim = 10;
int dataDim = 5;
Array A = (Array)malloc(sizeof(struct TCell)*10);
int * data = (int *)malloc(sizeof(int)*dataDim);
for(int j=0; j < arrayDim; j++){
A[j].data = (int *)malloc(sizeof(int)*dataDim);
for( int k = 0; k < dataDim; k++){
A[j].data[k] = rand()%100;
}
}
}
Exercise from: CS3008
Subscribe to:
Posts (Atom)