sys/wait.h and sys/kthread.h do not compile together - linux-kernel

I am compiling a kernel module in linux related to creating kthreads to achieve parallelism but I am stuck at compiling issues.
Here is my code:
#include <linux/init.h>
#include <linux/errno.h>
#include <asm/byteorder.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/types.h>
#include <sys/wait.h>
void threadfn1()
{
int j;
for( j = 0; j < 1000000; j++ )
printk(KERN_INFO "I AM THREAD 1 %d\n",j);
}
void threadfn2()
{
int j;
for( j = 0; j < 1000000; j++ )
printk(KERN_INFO "I AM THREAD 2 %d\n",j);
}
static int __init abc_init(void)
{
struct task_struct *t1 = kthread_run(threadfn1, NULL, "thread1");
struct task_struct *t2 = kthread_run(threadfn2, NULL, "thread2");
printk(KERN_INFO "HELLO WORLD\n");
waitpid(-1,NULL,0); // whatever the parameters of waitpid() are
}
static void __exit abc_fini(void)
{
printk(KERN_INFO "BYE WORLD\n");
}
module_init(abc_init);
module_exit(abc_fini);
The problem with my code is that when i compile my kernel module with make, sys/wait.h gives compiling errors like "redefinition of some strcut xyz" any many more errors, when linux/module.h and linux/kthread.h are also included. As soon as i comment out these two files, the module compiles well but gives a linking error that "waitpid" is undefined.
Why doesnt sys/wait.h compile well with linux/kthread.h and linux/module.h? Has anyone encountered this problem before?
Any help would be appreciated.

It is incorrect to include userspace headers, like sys/wait.h, in kernel code.

Related

How do you run code up until a certain line on Xcode?

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
cout << argv[0]; //ONLY WANT TO RUN TILL HERE
for(int x = 1; x < argc; x++)
{
string s(argv[x]);
if(
}
return 0;
}
I added and enabled a breakpoint on that line, but it still runs the whole program.
Screenshot of code:
You need to fix the errors in your code. The line with the red dot next to it has a syntax error that needs to be solved. Once you fix the error the program will run as desired.

GNU Readline library source code

I am trying to modify source code of the GNU Readline library in order to better undestrand how it works. I downloaded version 2.0 here and added an assert(0) statement inside rl_bind_key function in bind.c but it still works when I call this function in my program (main.c):
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "readline.h"
#include "history.h"
int main()
{
char* input, shell_prompt[100];
for (int i = 0; i < 100; i++) {
int val = rl_bind_key('\t', rl_complete);
printf(">> %d\n", val);
}
input = readline(shell_prompt);
if (!input)
break;
add_history(input);
}
Why does that happen?

Run Shared Memory program on cygwin

I have tried to run the following code on cygwin in windows; there will be no compilation errors in it but when I tried to run it I have Bad system call (core dumped) error.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
void main ( )
{ int shm_id; key_t mem_key;
int *shm_ptr;
mem_key = ftok(".", 'a');
shm_id = shmget(mem_key, sizeof(int), IPC_CREAT | 0666);
if (shm_id < 0)
{ printf("*** shmget error (server) ***\n");
exit(1);
}
shm_ptr = (int *) shmat(shm_id, NULL, 0);
/* attach */
if ((int) shm_ptr == -1)
{
printf("*** shmat error (server) ***\n");
exit(1);
}
}
Why I have this error? Can I solve it?
Thanks a lot.
You need to configure and start cygserver.

PDCURSES assignign value to integer make error

I'm using PDCURSES on Windows 7 and my program stops working at the beginning.
I noticed the error is made by assigning value to integer in line: 41 .
Unfortunately I have no idea why.
I'm writing in C in Code blocks.
I would be grateful if someone could help me.
Error message
#include <curses.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define ILOSC_NAZW 5
#define false 0
#define true 1
WINDOW * createwin(int height,int width,int starty,int startx);
void uaktualnij(WINDOW*left,char *nazwa);
void wyswietl_nazwy(WINDOW *right,char **losowe_nazwy);
int poprawnie=0,blednie=0;
int main()
{
char *losowe_nazwy[ILOSC_NAZW]={"JACK","TOM","KEN","JESSY","ANDRIEJ"};
char str[20];
int c,startx,starty,height,width;
WINDOW *right,*left,*console;
initscr();
refresh();
startx=0,starty=0,height=LINES/2,width=COLS/2;
left=createwin(height,width,starty,startx);
startx=COLS/2;
right=createwin(height,width,starty,startx);
startx=0;
starty=LINES/2;
width=COLS;
console=createwin(height,width,starty,startx);
getmaxyx(right,starty,startx);
uaktualnij(right,NULL);
mvwprintw(left,starty/2-starty/4,startx/2-strlen("PODAJ NAZWE UZYTKOWNIKA")/2,"PODAJ NAZWE UZYTKOWNIKA");
wrefresh(left);
wyswietl_nazwy(left,losowe_nazwy);
c=0; // assigning value make error
getmaxyx(console,starty,startx);
wmove(console,starty-2,2);
memset(str,0,sizeof *str);
wclear(console);
wborder(console,0,0,0,0,0,0,0,0);
wrefresh(console);
getch();
endwin();
return 0;
}
WINDOW * createwin(int height,int width,int starty,int startx){
WINDOW *mywin=newwin(height,width,starty,startx);
box(mywin,0,0);
wrefresh(mywin);
return mywin;
}
void czekaj(double sec){
clock_t koniec=clock()+sec*CLOCKS_PER_SEC;
while(clock()<koniec)continue;
}
void wyswietl_nazwy(WINDOW *right,char **losowe_nazwy){
int i=ILOSC_NAZW;
int rstartx,rstarty;
getmaxyx(right,rstarty,rstartx);
while( i>=0)
{
mvwprintw(right,rstarty/2-rstarty/4+2,rstartx/2-strlen(losowe_nazwy[i])/2,"%s",losowe_nazwy[i]);
wrefresh(right);
czekaj(0.2);
wmove(right,rstarty/2-rstarty/4+2,rstartx/2-strlen(losowe_nazwy[i]));
wclrtoeol(right);
mvwaddch(right,rstarty/2-rstarty/4+2,rstartx-1,ACS_VLINE);
wrefresh(right);
i--;
}
}
void uaktualnij(WINDOW*left,char *nazwa){
int startx,starty;
wclear(left);
wborder(left,0,0,0,0,0,0,0,0);
getyx(left,starty,startx);
mvwprintw(left,starty+1,startx+1,"Poprawnie: %i",poprawnie);
mvwprintw(left,starty+2,startx+1,"Blednie: %i",blednie);
if(nazwa!=NULL)
mvwprintw(left,starty+3,startx+1,"Wproawadzona nazwa uzytkowanika: %s",nazwa);
wrefresh(left);
}
Since PDCurses is written in C, I am assuming that you are compiling in C.
Move the
int c;
to just below the line
char *losowe_nazwy[ILOSC_NAZW]={"JACK","TOM","KEN","JESSY","ANDRIEJ"};
int c;
The C language expects those sorts of definitions to be at the top of the function.

boost.log auto_flush files are not stored when app is crashed

Recently I started to play with boost.log, and bumped into an issue that if an unhanded exception is thrown no log messages are written to the log file. I am using rolling text files and auto-flash option is set on.
Here is the modified source from the samples:
#include <stdexcept>
#include <string>
#include <iostream>
#include <fstream>
#include <functional>
#include <boost/ref.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/log/common.hpp>
#include <boost/log/filters.hpp>
#include <boost/log/formatters.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/utility/empty_deleter.hpp>
#include <boost/log/utility/record_ordering.hpp>
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace fmt = boost::log::formatters;
namespace keywords = boost::log::keywords;
using boost::shared_ptr;
using namespace boost::gregorian;
enum
{
LOG_RECORDS_TO_WRITE = 100,
LOG_RECORDS_TO_WRITE_BEFORE_EXCEPTION = 10,
THREAD_COUNT = 10
};
BOOST_LOG_DECLARE_GLOBAL_LOGGER(test_lg, src::logger_mt)
//! This function is executed in multiple threads
void thread_fun(boost::barrier& bar)
{
// Wait until all threads are created
bar.wait();
// Here we go. First, identify the thread.
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::thread::id, boost::this_thread::get_id());
// Now, do some logging
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
{
BOOST_LOG(get_test_lg()) << "Log record " << i;
if(i > LOG_RECORDS_TO_WRITE_BEFORE_EXCEPTION)
{
BOOST_THROW_EXCEPTION(std::exception("unhandled exception"));
}
}
}
int main(int argc, char* argv[])
{
try
{
typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
shared_ptr< file_sink > sink(new file_sink(
keywords::file_name = L"%Y%m%d_%H%M%S_%5N.log", // file name pattern
keywords::rotation_size = 10 * 1024 * 1024, // rotation size, in characters
keywords::auto_flush = true // make each log record flushed to the file
));
// Set up where the rotated files will be stored
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = "log" // where to store rotated files
));
// Upon restart, scan the target directory for files matching the file_name pattern
sink->locked_backend()->scan_for_files();
sink->locked_backend()->set_formatter(
fmt::format("%1%: [%2%] [%3%] - %4%")
% fmt::attr< unsigned int >("Line #")
% fmt::date_time< boost::posix_time::ptime >("TimeStamp")
% fmt::attr< boost::thread::id >("ThreadID")
% fmt::message()
);
// Add it to the core
logging::core::get()->add_sink(sink);
// Add some attributes too
shared_ptr< logging::attribute > attr(new attrs::local_clock);
logging::core::get()->add_global_attribute("TimeStamp", attr);
attr.reset(new attrs::counter< unsigned int >);
logging::core::get()->add_global_attribute("Line #", attr);
// Create logging threads
boost::barrier bar(THREAD_COUNT);
boost::thread_group threads;
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
// Wait until all action ends
threads.join_all();
return 0;
}
catch (std::exception& e)
{
std::cout << "FAILURE: " << e.what() << std::endl;
return 1;
}
}
Source is compiled under Visual Studio 2008. boost.log compiled for boost 1.40.
Any help is highly appreciated.
Check to see if the log file is in the current working directory of the process, rather than the specified file collector target directory ("log" in your sample code). Additionally, you will probably want to specify a directory for the sink "file_name" pattern.
As "JQ" notes, don't expect to see any logging post-exception.

Resources